{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1043,
   "metadata": {},
   "outputs": [],
   "source": [
    "setwd(\"/projects/PPC/analysis/ppc_eqtls\")\n",
    "\n",
    "source(\"scripts/packages.R\"  )\n",
    "source(\"scripts/functions.R\" )\n",
    "source(\"scripts/input_data.R\")\n",
    "\n",
    "library(coloc)\n",
    "\n",
    "set.seed(5366)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Objective: Re-annotate eQTLs based on LD"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Import 1000 Genomes rsids"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n",
      "11\n",
      "12\n",
      "13\n",
      "14\n",
      "15\n",
      "16\n",
      "17\n",
      "18\n",
      "19\n",
      "20\n",
      "21\n",
      "22\n"
     ]
    }
   ],
   "source": [
    "rsid_list = list()\n",
    "\n",
    "for (i in c(1:22))\n",
    "{\n",
    "    message(i, appendLF = F)\n",
    "    this = paste(\"VAR\", readLines(paste(\"/frazer01/home/jennifer/references/1kg/vcf\", paste(paste0(\"chr\", i), \"snp\", sep = \".\"), sep = \"/\")), sep = \"_\")\n",
    "    this = data.frame(snp = this)\n",
    "    this[,c(\"var\", \"chr\", \"pos\", \"ref\", \"alt\")] = str_split_fixed(this$snp, \"_\", 5)\n",
    "    this$chr_pos = paste(this$chr, this$pos, sep = \"_\")\n",
    "    rsid_list[[paste0(\"chr\", i)]] = this\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Get causal SNPs for each eQTL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading objects:\n",
      "  egene_list\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<style>\n",
       ".list-inline {list-style: none; margin:0; padding: 0}\n",
       ".list-inline>li {display: inline-block}\n",
       ".list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n",
       "</style>\n",
       "<ol class=list-inline><li>'gene'</li><li>'isoform'</li></ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 'gene'\n",
       "\\item 'isoform'\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 'gene'\n",
       "2. 'isoform'\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "[1] \"gene\"    \"isoform\""
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading objects:\n",
      "  gt1_list\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<style>\n",
       ".list-inline {list-style: none; margin:0; padding: 0}\n",
       ".list-inline>li {display: inline-block}\n",
       ".list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n",
       "</style>\n",
       "<ol class=list-inline><li>'gene'</li><li>'isoform'</li></ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 'gene'\n",
       "\\item 'isoform'\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 'gene'\n",
       "2. 'isoform'\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "[1] \"gene\"    \"isoform\""
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "load(\"manuscript/robj/egene_list.robj\", verbose = T)\n",
    "names(egene_list)\n",
    "\n",
    "load(\"manuscript/robj/gt1_list.robj\", verbose = T)\n",
    "names(gt1_list)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "# consolidate all eqtls\n",
    "all_qtls = rbindlist(lapply(names(egene_list), function(pheno) \n",
    "{\n",
    "    rbindlist(lapply(names(egene_list[[pheno]]), function(tiss)\n",
    "    {\n",
    "        egene_list[[pheno]][[tiss]] %>% mutate(qtl_id = gsub(\" \", \"_\", qtl_id)) %>% select(qtl_id) %>% mutate(tissue = tiss, analysis = pheno)\n",
    "    }))\n",
    "})) \n",
    "\n",
    "# rename tissues to be easier\n",
    "all_qtls[all_qtls$tissue == \"iPSC-PPC\",]$tissue = \"ipsc-ppc\"\n",
    "all_qtls[all_qtls$tissue == \"Islets\",]$tissue = \"islet\"\n",
    "all_qtls[all_qtls$tissue == \"Pancreas\",]$tissue = \"pancreas\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.table: 6 × 9</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>SNP.PP</th><th scope=col>snp</th><th scope=col>qtl_id</th><th scope=col>tissue</th><th scope=col>analysis</th><th scope=col>chrom</th><th scope=col>pos</th><th scope=col>type</th><th scope=col>transcript_id</th></tr>\n",
       "\t<tr><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><td>0.01067617</td><td>20_50005043_G_GA</td><td>ipsc-ppc_0_ENSG00000000419</td><td>ipsc-ppc</td><td>gene</td><td>20</td><td>50005043</td><td>0</td><td>ENSG00000000419</td></tr>\n",
       "\t<tr><td>0.03152537</td><td>6_46050368_G_C  </td><td>ipsc-ppc_0_ENSG00000001561</td><td>ipsc-ppc</td><td>gene</td><td> 6</td><td>46050368</td><td>0</td><td>ENSG00000001561</td></tr>\n",
       "\t<tr><td>0.01674834</td><td>6_46050598_T_A  </td><td>ipsc-ppc_0_ENSG00000001561</td><td>ipsc-ppc</td><td>gene</td><td> 6</td><td>46050598</td><td>0</td><td>ENSG00000001561</td></tr>\n",
       "\t<tr><td>0.01674834</td><td>6_46058361_C_T  </td><td>ipsc-ppc_0_ENSG00000001561</td><td>ipsc-ppc</td><td>gene</td><td> 6</td><td>46058361</td><td>0</td><td>ENSG00000001561</td></tr>\n",
       "\t<tr><td>0.02301381</td><td>6_46060370_TTG_T</td><td>ipsc-ppc_0_ENSG00000001561</td><td>ipsc-ppc</td><td>gene</td><td> 6</td><td>46060370</td><td>0</td><td>ENSG00000001561</td></tr>\n",
       "\t<tr><td>0.03152537</td><td>6_46064645_A_G  </td><td>ipsc-ppc_0_ENSG00000001561</td><td>ipsc-ppc</td><td>gene</td><td> 6</td><td>46064645</td><td>0</td><td>ENSG00000001561</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.table: 6 × 9\n",
       "\\begin{tabular}{lllllllll}\n",
       " SNP.PP & snp & qtl\\_id & tissue & analysis & chrom & pos & type & transcript\\_id\\\\\n",
       " <dbl> & <chr> & <chr> & <chr> & <chr> & <dbl> & <dbl> & <chr> & <chr>\\\\\n",
       "\\hline\n",
       "\t 0.01067617 & 20\\_50005043\\_G\\_GA & ipsc-ppc\\_0\\_ENSG00000000419 & ipsc-ppc & gene & 20 & 50005043 & 0 & ENSG00000000419\\\\\n",
       "\t 0.03152537 & 6\\_46050368\\_G\\_C   & ipsc-ppc\\_0\\_ENSG00000001561 & ipsc-ppc & gene &  6 & 46050368 & 0 & ENSG00000001561\\\\\n",
       "\t 0.01674834 & 6\\_46050598\\_T\\_A   & ipsc-ppc\\_0\\_ENSG00000001561 & ipsc-ppc & gene &  6 & 46050598 & 0 & ENSG00000001561\\\\\n",
       "\t 0.01674834 & 6\\_46058361\\_C\\_T   & ipsc-ppc\\_0\\_ENSG00000001561 & ipsc-ppc & gene &  6 & 46058361 & 0 & ENSG00000001561\\\\\n",
       "\t 0.02301381 & 6\\_46060370\\_TTG\\_T & ipsc-ppc\\_0\\_ENSG00000001561 & ipsc-ppc & gene &  6 & 46060370 & 0 & ENSG00000001561\\\\\n",
       "\t 0.03152537 & 6\\_46064645\\_A\\_G   & ipsc-ppc\\_0\\_ENSG00000001561 & ipsc-ppc & gene &  6 & 46064645 & 0 & ENSG00000001561\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.table: 6 × 9\n",
       "\n",
       "| SNP.PP &lt;dbl&gt; | snp &lt;chr&gt; | qtl_id &lt;chr&gt; | tissue &lt;chr&gt; | analysis &lt;chr&gt; | chrom &lt;dbl&gt; | pos &lt;dbl&gt; | type &lt;chr&gt; | transcript_id &lt;chr&gt; |\n",
       "|---|---|---|---|---|---|---|---|---|\n",
       "| 0.01067617 | 20_50005043_G_GA | ipsc-ppc_0_ENSG00000000419 | ipsc-ppc | gene | 20 | 50005043 | 0 | ENSG00000000419 |\n",
       "| 0.03152537 | 6_46050368_G_C   | ipsc-ppc_0_ENSG00000001561 | ipsc-ppc | gene |  6 | 46050368 | 0 | ENSG00000001561 |\n",
       "| 0.01674834 | 6_46050598_T_A   | ipsc-ppc_0_ENSG00000001561 | ipsc-ppc | gene |  6 | 46050598 | 0 | ENSG00000001561 |\n",
       "| 0.01674834 | 6_46058361_C_T   | ipsc-ppc_0_ENSG00000001561 | ipsc-ppc | gene |  6 | 46058361 | 0 | ENSG00000001561 |\n",
       "| 0.02301381 | 6_46060370_TTG_T | ipsc-ppc_0_ENSG00000001561 | ipsc-ppc | gene |  6 | 46060370 | 0 | ENSG00000001561 |\n",
       "| 0.03152537 | 6_46064645_A_G   | ipsc-ppc_0_ENSG00000001561 | ipsc-ppc | gene |  6 | 46064645 | 0 | ENSG00000001561 |\n",
       "\n"
      ],
      "text/plain": [
       "  SNP.PP     snp              qtl_id                     tissue   analysis\n",
       "1 0.01067617 20_50005043_G_GA ipsc-ppc_0_ENSG00000000419 ipsc-ppc gene    \n",
       "2 0.03152537 6_46050368_G_C   ipsc-ppc_0_ENSG00000001561 ipsc-ppc gene    \n",
       "3 0.01674834 6_46050598_T_A   ipsc-ppc_0_ENSG00000001561 ipsc-ppc gene    \n",
       "4 0.01674834 6_46058361_C_T   ipsc-ppc_0_ENSG00000001561 ipsc-ppc gene    \n",
       "5 0.02301381 6_46060370_TTG_T ipsc-ppc_0_ENSG00000001561 ipsc-ppc gene    \n",
       "6 0.03152537 6_46064645_A_G   ipsc-ppc_0_ENSG00000001561 ipsc-ppc gene    \n",
       "  chrom pos      type transcript_id  \n",
       "1 20    50005043 0    ENSG00000000419\n",
       "2  6    46050368 0    ENSG00000001561\n",
       "3  6    46050598 0    ENSG00000001561\n",
       "4  6    46058361 0    ENSG00000001561\n",
       "5  6    46060370 0    ENSG00000001561\n",
       "6  6    46064645 0    ENSG00000001561"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# consolidate all fine-mapped snps with pp > 1%\n",
    "snps_gt1 = rbindlist(lapply(names(gt1_list), function(pheno)\n",
    "{\n",
    "    rbindlist(lapply(names(gt1_list[[pheno]]), function(tiss)\n",
    "    {\n",
    "        gt1_list[[pheno]][[tiss]] %>% select(SNP.PP, snp, qtl_id) %>% mutate(tissue = tiss, analysis = pheno) %>% filter(SNP.PP >= 0.01)\n",
    "    }))\n",
    "})) %>% mutate(snp = gsub(\"VAR_\", \"\", snp), chrom = unlist(lapply(snp, function(x) { unlist(strsplit(x, \"_\"))[1] })))\n",
    "\n",
    "# get chrom and pos\n",
    "snps_gt1$chrom = as.numeric(snps_gt1$chrom)\n",
    "snps_gt1$pos = as.numeric(unlist(lapply(snps_gt1$snp, function(x) { unlist(strsplit(x, \"_\"))[2] })))\n",
    "\n",
    "# rename tissue\n",
    "snps_gt1[snps_gt1$tissue == \"iPSC-PPC\",]$tissue = \"ipsc-ppc\"\n",
    "snps_gt1[snps_gt1$tissue == \"Islets\",]$tissue = \"islet\"\n",
    "snps_gt1[snps_gt1$tissue == \"Pancreas\",]$tissue = \"pancreas\"\n",
    "\n",
    "# get discovery order\n",
    "snps_gt1$type = unlist(lapply(snps_gt1$qtl_id, function(x) { unlist(strsplit(x, \"_\"))[1] }))\n",
    "snps_gt1[snps_gt1$type %in% c(\"Islets\", \"Pancreas\"),]$type = 0\n",
    "\n",
    "# get transcript_id\n",
    "snps_gt1$transcript_id = unlist(lapply(snps_gt1$qtl_id, function(x) { unlist(strsplit(x, \"_\"))[2] }))\n",
    "\n",
    "# snps for only eqtls that passed criteria\n",
    "snps_gt1 = snps_gt1 %>% filter(qtl_id %in% all_qtls$qtl_id)\n",
    "\n",
    "head(snps_gt1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "         \n",
       "          ipsc-ppc  islet pancreas\n",
       "  gene       57771  70141   116309\n",
       "  isoform    54787 106363    29453"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "table(snps_gt1$analysis, snps_gt1$tissue)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Warning message in cbind(parts$left, chars$ellip_h, parts$right, deparse.level = 0L):\n",
      "“number of rows of result is not a multiple of vector length (arg 2)”\n",
      "Warning message in cbind(parts$left, chars$ellip_h, parts$right, deparse.level = 0L):\n",
      "“number of rows of result is not a multiple of vector length (arg 2)”\n",
      "Warning message in cbind(parts$left, chars$ellip_h, parts$right, deparse.level = 0L):\n",
      "“number of rows of result is not a multiple of vector length (arg 2)”\n",
      "Warning message in cbind(parts$left, chars$ellip_h, parts$right, deparse.level = 0L):\n",
      "“number of rows of result is not a multiple of vector length (arg 2)”\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 0 × 22</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>module_id</th><th scope=col>eqtl_id</th><th scope=col>transcript_id</th><th scope=col>gene_id</th><th scope=col>gene_name</th><th scope=col>developmental_stage</th><th scope=col>tissue</th><th scope=col>eqtl_phenotype</th><th scope=col>eqtl_type</th><th scope=col>expressed_ipsc_ppc</th><th scope=col>⋯</th><th scope=col>LD_ipsc_ppc</th><th scope=col>LD_islets</th><th scope=col>LD_pancreas</th><th scope=col>LD_islets_conditional</th><th scope=col>LD_pancreas_conditional</th><th scope=col>islet_egene_overlap</th><th scope=col>pancreas_egene_overlap</th><th scope=col>module_pass</th><th scope=col>category_annotation</th><th scope=col>notes</th></tr>\n",
       "\t<tr><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;lgl&gt;</th><th scope=col>⋯</th><th scope=col>&lt;lgl&gt;</th><th scope=col>&lt;lgl&gt;</th><th scope=col>&lt;lgl&gt;</th><th scope=col>&lt;lgl&gt;</th><th scope=col>&lt;lgl&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;lgl&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 0 × 22\n",
       "\\begin{tabular}{lllllllllllllllllllll}\n",
       " module\\_id & eqtl\\_id & transcript\\_id & gene\\_id & gene\\_name & developmental\\_stage & tissue & eqtl\\_phenotype & eqtl\\_type & expressed\\_ipsc\\_ppc & ⋯ & LD\\_ipsc\\_ppc & LD\\_islets & LD\\_pancreas & LD\\_islets\\_conditional & LD\\_pancreas\\_conditional & islet\\_egene\\_overlap & pancreas\\_egene\\_overlap & module\\_pass & category\\_annotation & notes\\\\\n",
       " <chr> & <chr> & <chr> & <chr> & <chr> & <chr> & <chr> & <chr> & <chr> & <lgl> & ⋯ & <lgl> & <lgl> & <lgl> & <lgl> & <lgl> & <chr> & <chr> & <lgl> & <chr> & <chr>\\\\\n",
       "\\hline\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 0 × 22\n",
       "\n",
       "| module_id &lt;chr&gt; | eqtl_id &lt;chr&gt; | transcript_id &lt;chr&gt; | gene_id &lt;chr&gt; | gene_name &lt;chr&gt; | developmental_stage &lt;chr&gt; | tissue &lt;chr&gt; | eqtl_phenotype &lt;chr&gt; | eqtl_type &lt;chr&gt; | expressed_ipsc_ppc &lt;lgl&gt; | ⋯ ⋯ | LD_ipsc_ppc &lt;lgl&gt; | LD_islets &lt;lgl&gt; | LD_pancreas &lt;lgl&gt; | LD_islets_conditional &lt;lgl&gt; | LD_pancreas_conditional &lt;lgl&gt; | islet_egene_overlap &lt;chr&gt; | pancreas_egene_overlap &lt;chr&gt; | module_pass &lt;lgl&gt; | category_annotation &lt;chr&gt; | notes &lt;chr&gt; |\n",
       "|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n",
       "\n"
      ],
      "text/plain": [
       "     module_id eqtl_id transcript_id gene_id gene_name developmental_stage\n",
       "     tissue eqtl_phenotype eqtl_type expressed_ipsc_ppc ⋯ LD_ipsc_ppc LD_islets\n",
       "     LD_pancreas LD_islets_conditional LD_pancreas_conditional\n",
       "     islet_egene_overlap pancreas_egene_overlap module_pass category_annotation\n",
       "     notes"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# check that all eQTLs are present\n",
    "eqtl_annot = fread(\"reviews/tables/Table_S11_eQTL_Annotations.txt\", data.table = F)\n",
    "eqtl_annot %>% filter(!eqtl_id %in% snps_gt1$qtl_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Get adult conditional lead snps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading objects:\n",
      "  cond_list\n"
     ]
    }
   ],
   "source": [
    "load(\"input/eqtl/pancreas_islets_conditionals.robj\", verbose = T)\n",
    "conds = fread(\"input/eqtl/pancreas_islets_conditionals.txt\", data.table = F)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "# reformat, arbitrarily assign SNP.PP 1% \n",
    "conds2 = conds %>% mutate(snp = hg19_snp, qtl_id = paste(tissue, rank, phenotype_id, sep = \"_\")) %>% \n",
    "    mutate(SNP.PP = 0.01, type = rank) %>% dplyr::rename(chrom = chr) %>% select(SNP.PP, type, snp, qtl_id, tissue, analysis, chrom, pos)\n",
    "\n",
    "# rename tissues\n",
    "conds2[conds2$tissue == \"Pancreas\",]$tissue = \"pancreas\"\n",
    "conds2[conds2$tissue == \"Islets\",]$tissue = \"islet\"\n",
    "\n",
    "# rename qtl_ids\n",
    "conds2$qtl_id = gsub(\"Pancreas\", \"pancreas\", conds2$qtl_id)\n",
    "conds2$qtl_id = gsub(\"Islets\", \"islet\", conds2$qtl_id)\n",
    "\n",
    "# add analysis and transcript_id column\n",
    "conds2$analysis = ifelse(conds$analysis %like% \"gene\", \"gene\", \"isoform\")\n",
    "conds2$transcript_id = unlist(lapply(conds2$qtl_id, function(x) { x = unlist(strsplit(x, \"_\")); paste(x[3:length(x)], collapse = \"_\") }))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [],
      "text/latex": [],
      "text/markdown": [],
      "text/plain": [
       "character(0)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [],
      "text/latex": [],
      "text/markdown": [],
      "text/plain": [
       "character(0)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# make sure the columns are the same with snps_gt1\n",
    "setdiff(colnames(conds2), colnames(snps_gt1))\n",
    "setdiff(colnames(snps_gt1), colnames(conds2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 2 × 9</caption>\n",
       "<thead>\n",
       "\t<tr><th></th><th scope=col>SNP.PP</th><th scope=col>type</th><th scope=col>snp</th><th scope=col>qtl_id</th><th scope=col>tissue</th><th scope=col>analysis</th><th scope=col>chrom</th><th scope=col>pos</th><th scope=col>transcript_id</th></tr>\n",
       "\t<tr><th></th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;int&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;int&gt;</th><th scope=col>&lt;int&gt;</th><th scope=col>&lt;chr&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><th scope=row>1</th><td>0.01</td><td>1</td><td>1_100650555_T_C</td><td>pancreas_1_ENSG00000122477</td><td>pancreas</td><td>gene</td><td>1</td><td>100650555</td><td>ENSG00000122477</td></tr>\n",
       "\t<tr><th scope=row>2</th><td>0.01</td><td>1</td><td>1_100717447_G_T</td><td>pancreas_1_ENSG00000137996</td><td>pancreas</td><td>gene</td><td>1</td><td>100717447</td><td>ENSG00000137996</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 2 × 9\n",
       "\\begin{tabular}{r|lllllllll}\n",
       "  & SNP.PP & type & snp & qtl\\_id & tissue & analysis & chrom & pos & transcript\\_id\\\\\n",
       "  & <dbl> & <int> & <chr> & <chr> & <chr> & <chr> & <int> & <int> & <chr>\\\\\n",
       "\\hline\n",
       "\t1 & 0.01 & 1 & 1\\_100650555\\_T\\_C & pancreas\\_1\\_ENSG00000122477 & pancreas & gene & 1 & 100650555 & ENSG00000122477\\\\\n",
       "\t2 & 0.01 & 1 & 1\\_100717447\\_G\\_T & pancreas\\_1\\_ENSG00000137996 & pancreas & gene & 1 & 100717447 & ENSG00000137996\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 2 × 9\n",
       "\n",
       "| <!--/--> | SNP.PP &lt;dbl&gt; | type &lt;int&gt; | snp &lt;chr&gt; | qtl_id &lt;chr&gt; | tissue &lt;chr&gt; | analysis &lt;chr&gt; | chrom &lt;int&gt; | pos &lt;int&gt; | transcript_id &lt;chr&gt; |\n",
       "|---|---|---|---|---|---|---|---|---|---|\n",
       "| 1 | 0.01 | 1 | 1_100650555_T_C | pancreas_1_ENSG00000122477 | pancreas | gene | 1 | 100650555 | ENSG00000122477 |\n",
       "| 2 | 0.01 | 1 | 1_100717447_G_T | pancreas_1_ENSG00000137996 | pancreas | gene | 1 | 100717447 | ENSG00000137996 |\n",
       "\n"
      ],
      "text/plain": [
       "  SNP.PP type snp             qtl_id                     tissue   analysis\n",
       "1 0.01   1    1_100650555_T_C pancreas_1_ENSG00000122477 pancreas gene    \n",
       "2 0.01   1    1_100717447_G_T pancreas_1_ENSG00000137996 pancreas gene    \n",
       "  chrom pos       transcript_id  \n",
       "1 1     100650555 ENSG00000122477\n",
       "2 1     100717447 ENSG00000137996"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.table: 2 × 9</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>SNP.PP</th><th scope=col>snp</th><th scope=col>qtl_id</th><th scope=col>tissue</th><th scope=col>analysis</th><th scope=col>chrom</th><th scope=col>pos</th><th scope=col>type</th><th scope=col>transcript_id</th></tr>\n",
       "\t<tr><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><td>0.01067617</td><td>20_50005043_G_GA</td><td>ipsc-ppc_0_ENSG00000000419</td><td>ipsc-ppc</td><td>gene</td><td>20</td><td>50005043</td><td>0</td><td>ENSG00000000419</td></tr>\n",
       "\t<tr><td>0.03152537</td><td>6_46050368_G_C  </td><td>ipsc-ppc_0_ENSG00000001561</td><td>ipsc-ppc</td><td>gene</td><td> 6</td><td>46050368</td><td>0</td><td>ENSG00000001561</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.table: 2 × 9\n",
       "\\begin{tabular}{lllllllll}\n",
       " SNP.PP & snp & qtl\\_id & tissue & analysis & chrom & pos & type & transcript\\_id\\\\\n",
       " <dbl> & <chr> & <chr> & <chr> & <chr> & <dbl> & <dbl> & <chr> & <chr>\\\\\n",
       "\\hline\n",
       "\t 0.01067617 & 20\\_50005043\\_G\\_GA & ipsc-ppc\\_0\\_ENSG00000000419 & ipsc-ppc & gene & 20 & 50005043 & 0 & ENSG00000000419\\\\\n",
       "\t 0.03152537 & 6\\_46050368\\_G\\_C   & ipsc-ppc\\_0\\_ENSG00000001561 & ipsc-ppc & gene &  6 & 46050368 & 0 & ENSG00000001561\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.table: 2 × 9\n",
       "\n",
       "| SNP.PP &lt;dbl&gt; | snp &lt;chr&gt; | qtl_id &lt;chr&gt; | tissue &lt;chr&gt; | analysis &lt;chr&gt; | chrom &lt;dbl&gt; | pos &lt;dbl&gt; | type &lt;chr&gt; | transcript_id &lt;chr&gt; |\n",
       "|---|---|---|---|---|---|---|---|---|\n",
       "| 0.01067617 | 20_50005043_G_GA | ipsc-ppc_0_ENSG00000000419 | ipsc-ppc | gene | 20 | 50005043 | 0 | ENSG00000000419 |\n",
       "| 0.03152537 | 6_46050368_G_C   | ipsc-ppc_0_ENSG00000001561 | ipsc-ppc | gene |  6 | 46050368 | 0 | ENSG00000001561 |\n",
       "\n"
      ],
      "text/plain": [
       "  SNP.PP     snp              qtl_id                     tissue   analysis\n",
       "1 0.01067617 20_50005043_G_GA ipsc-ppc_0_ENSG00000000419 ipsc-ppc gene    \n",
       "2 0.03152537 6_46050368_G_C   ipsc-ppc_0_ENSG00000001561 ipsc-ppc gene    \n",
       "  chrom pos      type transcript_id  \n",
       "1 20    50005043 0    ENSG00000000419\n",
       "2  6    46050368 0    ENSG00000001561"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "head(conds2,2)\n",
    "head(snps_gt1,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "snps_gt1 = rbind(snps_gt1, conds2) %>% distinct()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Check that all eQTLs are present"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get all adult conditional eqtls\n",
    "cond_list[[\"panc_eqtl\"]]  = cond_list[[\"panc_eqtl\" ]] %>% mutate(qtl_id = paste(\"pancreas\", rank, simplify_id(phenotype_id), sep = \"_\"))\n",
    "cond_list[[\"panc_sqtl\"]]  = cond_list[[\"panc_sqtl\" ]] %>% mutate(qtl_id = paste(\"pancreas\", rank, phenotype_id             , sep = \"_\"))\n",
    "cond_list[[\"islet_eqtl\"]] = cond_list[[\"islet_eqtl\"]] %>% mutate(qtl_id = paste(\"islet\"   , rank, simplify_id(phenotype_id), sep = \"_\"))\n",
    "cond_list[[\"islet_sqtl\"]] = cond_list[[\"islet_sqtl\"]] %>% mutate(qtl_id = paste(\"islet\"   , rank, phenotype_id             , sep = \"_\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 6 × 7</caption>\n",
       "<thead>\n",
       "\t<tr><th></th><th scope=col>hg19_snp</th><th scope=col>rank</th><th scope=col>phenotype_id</th><th scope=col>chr</th><th scope=col>tissue</th><th scope=col>analysis</th><th scope=col>qtl_id</th></tr>\n",
       "\t<tr><th></th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;int&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><th scope=row>1</th><td>3_53909731_C_A</td><td>1</td><td>ENSG00000113812.9 </td><td>3</td><td>islets</td><td>gene</td><td>islet_1_ENSG00000113812</td></tr>\n",
       "\t<tr><th scope=row>2</th><td>3_56734630_C_T</td><td>1</td><td>ENSG00000187672.8 </td><td>3</td><td>islets</td><td>gene</td><td>islet_1_ENSG00000187672</td></tr>\n",
       "\t<tr><th scope=row>3</th><td>3_56726105_A_C</td><td>1</td><td>ENSG00000180376.12</td><td>3</td><td>islets</td><td>gene</td><td>islet_1_ENSG00000180376</td></tr>\n",
       "\t<tr><th scope=row>4</th><td>3_56793722_A_G</td><td>1</td><td>ENSG00000163947.7 </td><td>3</td><td>islets</td><td>gene</td><td>islet_1_ENSG00000163947</td></tr>\n",
       "\t<tr><th scope=row>5</th><td>3_58280061_T_C</td><td>1</td><td>ENSG00000163686.9 </td><td>3</td><td>islets</td><td>gene</td><td>islet_1_ENSG00000163686</td></tr>\n",
       "\t<tr><th scope=row>6</th><td>3_58277536_A_G</td><td>1</td><td>ENSG00000168297.11</td><td>3</td><td>islets</td><td>gene</td><td>islet_1_ENSG00000168297</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 6 × 7\n",
       "\\begin{tabular}{r|lllllll}\n",
       "  & hg19\\_snp & rank & phenotype\\_id & chr & tissue & analysis & qtl\\_id\\\\\n",
       "  & <chr> & <int> & <chr> & <chr> & <chr> & <chr> & <chr>\\\\\n",
       "\\hline\n",
       "\t1 & 3\\_53909731\\_C\\_A & 1 & ENSG00000113812.9  & 3 & islets & gene & islet\\_1\\_ENSG00000113812\\\\\n",
       "\t2 & 3\\_56734630\\_C\\_T & 1 & ENSG00000187672.8  & 3 & islets & gene & islet\\_1\\_ENSG00000187672\\\\\n",
       "\t3 & 3\\_56726105\\_A\\_C & 1 & ENSG00000180376.12 & 3 & islets & gene & islet\\_1\\_ENSG00000180376\\\\\n",
       "\t4 & 3\\_56793722\\_A\\_G & 1 & ENSG00000163947.7  & 3 & islets & gene & islet\\_1\\_ENSG00000163947\\\\\n",
       "\t5 & 3\\_58280061\\_T\\_C & 1 & ENSG00000163686.9  & 3 & islets & gene & islet\\_1\\_ENSG00000163686\\\\\n",
       "\t6 & 3\\_58277536\\_A\\_G & 1 & ENSG00000168297.11 & 3 & islets & gene & islet\\_1\\_ENSG00000168297\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 6 × 7\n",
       "\n",
       "| <!--/--> | hg19_snp &lt;chr&gt; | rank &lt;int&gt; | phenotype_id &lt;chr&gt; | chr &lt;chr&gt; | tissue &lt;chr&gt; | analysis &lt;chr&gt; | qtl_id &lt;chr&gt; |\n",
       "|---|---|---|---|---|---|---|---|\n",
       "| 1 | 3_53909731_C_A | 1 | ENSG00000113812.9  | 3 | islets | gene | islet_1_ENSG00000113812 |\n",
       "| 2 | 3_56734630_C_T | 1 | ENSG00000187672.8  | 3 | islets | gene | islet_1_ENSG00000187672 |\n",
       "| 3 | 3_56726105_A_C | 1 | ENSG00000180376.12 | 3 | islets | gene | islet_1_ENSG00000180376 |\n",
       "| 4 | 3_56793722_A_G | 1 | ENSG00000163947.7  | 3 | islets | gene | islet_1_ENSG00000163947 |\n",
       "| 5 | 3_58280061_T_C | 1 | ENSG00000163686.9  | 3 | islets | gene | islet_1_ENSG00000163686 |\n",
       "| 6 | 3_58277536_A_G | 1 | ENSG00000168297.11 | 3 | islets | gene | islet_1_ENSG00000168297 |\n",
       "\n"
      ],
      "text/plain": [
       "  hg19_snp       rank phenotype_id       chr tissue analysis\n",
       "1 3_53909731_C_A 1    ENSG00000113812.9  3   islets gene    \n",
       "2 3_56734630_C_T 1    ENSG00000187672.8  3   islets gene    \n",
       "3 3_56726105_A_C 1    ENSG00000180376.12 3   islets gene    \n",
       "4 3_56793722_A_G 1    ENSG00000163947.7  3   islets gene    \n",
       "5 3_58280061_T_C 1    ENSG00000163686.9  3   islets gene    \n",
       "6 3_58277536_A_G 1    ENSG00000168297.11 3   islets gene    \n",
       "  qtl_id                 \n",
       "1 islet_1_ENSG00000113812\n",
       "2 islet_1_ENSG00000187672\n",
       "3 islet_1_ENSG00000180376\n",
       "4 islet_1_ENSG00000163947\n",
       "5 islet_1_ENSG00000163686\n",
       "6 islet_1_ENSG00000168297"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "head(cond_list[[\"islet_eqtl\"]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 0 × 7</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>hg19_snp</th><th scope=col>rank</th><th scope=col>phenotype_id</th><th scope=col>chr</th><th scope=col>tissue</th><th scope=col>analysis</th><th scope=col>qtl_id</th></tr>\n",
       "\t<tr><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;int&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 0 × 7\n",
       "\\begin{tabular}{lllllll}\n",
       " hg19\\_snp & rank & phenotype\\_id & chr & tissue & analysis & qtl\\_id\\\\\n",
       " <chr> & <int> & <chr> & <chr> & <chr> & <chr> & <chr>\\\\\n",
       "\\hline\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 0 × 7\n",
       "\n",
       "| hg19_snp &lt;chr&gt; | rank &lt;int&gt; | phenotype_id &lt;chr&gt; | chr &lt;chr&gt; | tissue &lt;chr&gt; | analysis &lt;chr&gt; | qtl_id &lt;chr&gt; |\n",
       "|---|---|---|---|---|---|---|\n",
       "\n"
      ],
      "text/plain": [
       "     hg19_snp rank phenotype_id chr tissue analysis qtl_id"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 0 × 7</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>hg19_snp</th><th scope=col>rank</th><th scope=col>phenotype_id</th><th scope=col>chr</th><th scope=col>tissue</th><th scope=col>analysis</th><th scope=col>qtl_id</th></tr>\n",
       "\t<tr><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;int&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 0 × 7\n",
       "\\begin{tabular}{lllllll}\n",
       " hg19\\_snp & rank & phenotype\\_id & chr & tissue & analysis & qtl\\_id\\\\\n",
       " <chr> & <int> & <chr> & <chr> & <chr> & <chr> & <chr>\\\\\n",
       "\\hline\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 0 × 7\n",
       "\n",
       "| hg19_snp &lt;chr&gt; | rank &lt;int&gt; | phenotype_id &lt;chr&gt; | chr &lt;chr&gt; | tissue &lt;chr&gt; | analysis &lt;chr&gt; | qtl_id &lt;chr&gt; |\n",
       "|---|---|---|---|---|---|---|\n",
       "\n"
      ],
      "text/plain": [
       "     hg19_snp rank phenotype_id chr tissue analysis qtl_id"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 0 × 7</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>hg19_snp</th><th scope=col>rank</th><th scope=col>phenotype_id</th><th scope=col>chr</th><th scope=col>tissue</th><th scope=col>analysis</th><th scope=col>qtl_id</th></tr>\n",
       "\t<tr><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;int&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 0 × 7\n",
       "\\begin{tabular}{lllllll}\n",
       " hg19\\_snp & rank & phenotype\\_id & chr & tissue & analysis & qtl\\_id\\\\\n",
       " <chr> & <int> & <chr> & <chr> & <chr> & <chr> & <chr>\\\\\n",
       "\\hline\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 0 × 7\n",
       "\n",
       "| hg19_snp &lt;chr&gt; | rank &lt;int&gt; | phenotype_id &lt;chr&gt; | chr &lt;chr&gt; | tissue &lt;chr&gt; | analysis &lt;chr&gt; | qtl_id &lt;chr&gt; |\n",
       "|---|---|---|---|---|---|---|\n",
       "\n"
      ],
      "text/plain": [
       "     hg19_snp rank phenotype_id chr tissue analysis qtl_id"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 0 × 7</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>hg19_snp</th><th scope=col>rank</th><th scope=col>phenotype_id</th><th scope=col>chr</th><th scope=col>tissue</th><th scope=col>analysis</th><th scope=col>qtl_id</th></tr>\n",
       "\t<tr><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;int&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 0 × 7\n",
       "\\begin{tabular}{lllllll}\n",
       " hg19\\_snp & rank & phenotype\\_id & chr & tissue & analysis & qtl\\_id\\\\\n",
       " <chr> & <int> & <chr> & <chr> & <chr> & <chr> & <chr>\\\\\n",
       "\\hline\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 0 × 7\n",
       "\n",
       "| hg19_snp &lt;chr&gt; | rank &lt;int&gt; | phenotype_id &lt;chr&gt; | chr &lt;chr&gt; | tissue &lt;chr&gt; | analysis &lt;chr&gt; | qtl_id &lt;chr&gt; |\n",
       "|---|---|---|---|---|---|---|\n",
       "\n"
      ],
      "text/plain": [
       "     hg19_snp rank phenotype_id chr tissue analysis qtl_id"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# check that there are no sex chromosomes - we are only considering autosomes\n",
    "cond_list[[\"panc_eqtl\" ]] %>% filter(!qtl_id %in% snps_gt1$qtl_id) %>% filter(chr != \"X\")\n",
    "cond_list[[\"panc_sqtl\" ]] %>% filter(!qtl_id %in% snps_gt1$qtl_id) %>% filter(chr != \"X\")\n",
    "cond_list[[\"islet_eqtl\"]] %>% filter(!qtl_id %in% snps_gt1$qtl_id) %>% filter(chr != \"23\")\n",
    "cond_list[[\"islet_sqtl\"]] %>% filter(!qtl_id %in% snps_gt1$qtl_id) %>% filter(chr != \"23\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Warning message in cbind(parts$left, chars$ellip_h, parts$right, deparse.level = 0L):\n",
      "“number of rows of result is not a multiple of vector length (arg 2)”\n",
      "Warning message in cbind(parts$left, chars$ellip_h, parts$right, deparse.level = 0L):\n",
      "“number of rows of result is not a multiple of vector length (arg 2)”\n",
      "Warning message in cbind(parts$left, chars$ellip_h, parts$right, deparse.level = 0L):\n",
      "“number of rows of result is not a multiple of vector length (arg 2)”\n",
      "Warning message in cbind(parts$left, chars$ellip_h, parts$right, deparse.level = 0L):\n",
      "“number of rows of result is not a multiple of vector length (arg 2)”\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 0 × 22</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>module_id</th><th scope=col>eqtl_id</th><th scope=col>transcript_id</th><th scope=col>gene_id</th><th scope=col>gene_name</th><th scope=col>developmental_stage</th><th scope=col>tissue</th><th scope=col>eqtl_phenotype</th><th scope=col>eqtl_type</th><th scope=col>expressed_ipsc_ppc</th><th scope=col>⋯</th><th scope=col>LD_ipsc_ppc</th><th scope=col>LD_islets</th><th scope=col>LD_pancreas</th><th scope=col>LD_islets_conditional</th><th scope=col>LD_pancreas_conditional</th><th scope=col>islet_egene_overlap</th><th scope=col>pancreas_egene_overlap</th><th scope=col>module_pass</th><th scope=col>category_annotation</th><th scope=col>notes</th></tr>\n",
       "\t<tr><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;lgl&gt;</th><th scope=col>⋯</th><th scope=col>&lt;lgl&gt;</th><th scope=col>&lt;lgl&gt;</th><th scope=col>&lt;lgl&gt;</th><th scope=col>&lt;lgl&gt;</th><th scope=col>&lt;lgl&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;lgl&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 0 × 22\n",
       "\\begin{tabular}{lllllllllllllllllllll}\n",
       " module\\_id & eqtl\\_id & transcript\\_id & gene\\_id & gene\\_name & developmental\\_stage & tissue & eqtl\\_phenotype & eqtl\\_type & expressed\\_ipsc\\_ppc & ⋯ & LD\\_ipsc\\_ppc & LD\\_islets & LD\\_pancreas & LD\\_islets\\_conditional & LD\\_pancreas\\_conditional & islet\\_egene\\_overlap & pancreas\\_egene\\_overlap & module\\_pass & category\\_annotation & notes\\\\\n",
       " <chr> & <chr> & <chr> & <chr> & <chr> & <chr> & <chr> & <chr> & <chr> & <lgl> & ⋯ & <lgl> & <lgl> & <lgl> & <lgl> & <lgl> & <chr> & <chr> & <lgl> & <chr> & <chr>\\\\\n",
       "\\hline\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 0 × 22\n",
       "\n",
       "| module_id &lt;chr&gt; | eqtl_id &lt;chr&gt; | transcript_id &lt;chr&gt; | gene_id &lt;chr&gt; | gene_name &lt;chr&gt; | developmental_stage &lt;chr&gt; | tissue &lt;chr&gt; | eqtl_phenotype &lt;chr&gt; | eqtl_type &lt;chr&gt; | expressed_ipsc_ppc &lt;lgl&gt; | ⋯ ⋯ | LD_ipsc_ppc &lt;lgl&gt; | LD_islets &lt;lgl&gt; | LD_pancreas &lt;lgl&gt; | LD_islets_conditional &lt;lgl&gt; | LD_pancreas_conditional &lt;lgl&gt; | islet_egene_overlap &lt;chr&gt; | pancreas_egene_overlap &lt;chr&gt; | module_pass &lt;lgl&gt; | category_annotation &lt;chr&gt; | notes &lt;chr&gt; |\n",
       "|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n",
       "\n"
      ],
      "text/plain": [
       "     module_id eqtl_id transcript_id gene_id gene_name developmental_stage\n",
       "     tissue eqtl_phenotype eqtl_type expressed_ipsc_ppc ⋯ LD_ipsc_ppc LD_islets\n",
       "     LD_pancreas LD_islets_conditional LD_pancreas_conditional\n",
       "     islet_egene_overlap pancreas_egene_overlap module_pass category_annotation\n",
       "     notes"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# check again that all ppc qtls and primary adults are present \n",
    "eqtl_annot = fread(\"reviews/tables/Table_S11_eQTL_Annotations.txt\", data.table = F)\n",
    "eqtl_annot %>% filter(!eqtl_id %in% snps_gt1$qtl_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Add rsids"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n",
      "11\n",
      "12\n",
      "13\n",
      "14\n",
      "15\n",
      "16\n",
      "17\n",
      "18\n",
      "19\n",
      "20\n",
      "21\n",
      "22\n"
     ]
    }
   ],
   "source": [
    "# reformat snp id as \"VAR_[chr]_[pos]_[ref]_[alt]\"\n",
    "snps_gt1$snp = paste(\"VAR\", snps_gt1$snp, sep = \"_\")\n",
    "\n",
    "# annotate each snp TRUE/FALSE if they are genotyped in 1000 Genomes\n",
    "snps_gt1 = as.data.frame(rbindlist(lapply(c(1:22), function(x)\n",
    "{\n",
    "    message(x, appendLF = F)\n",
    "    snps_gt1 %>% filter(chrom == x) %>% mutate(in_1kg = ifelse(snp %in% rsid_list[[paste0(\"chr\", x)]]$snp, T, F))\n",
    "})))\n",
    "\n",
    "# reformat snp id to remove \"VAR_\" in front\n",
    "snps_gt1$snp = gsub(\"VAR_\", \"\", snps_gt1$snp)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Choose SNPs to test LD with"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [],
   "source": [
    "# prioritize snps in 1000 Genomes, select the one with max SNP.PP, but if multiple SNPs have max SNP.PP, choose randomly\n",
    "top = snps_gt1 %>% filter(in_1kg == T) %>% group_by(qtl_id) %>% filter(SNP.PP == max(SNP.PP)) %>% filter(duplicated(qtl_id) == F)\n",
    "\n",
    "# for qtls with no snps in 1000 Genomes, select the snp with highest SNP.PP, \n",
    "top = rbind(top, snps_gt1 %>% filter(!qtl_id %in% top$qtl_id) %>% group_by(SNP.PP) %>% filter(SNP.PP == max(SNP.PP)) %>% filter(duplicated(qtl_id) == F))\n",
    "top = top %>% filter(duplicated(qtl_id) == F)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Warning message in cbind(parts$left, chars$ellip_h, parts$right, deparse.level = 0L):\n",
      "“number of rows of result is not a multiple of vector length (arg 2)”\n",
      "Warning message in cbind(parts$left, chars$ellip_h, parts$right, deparse.level = 0L):\n",
      "“number of rows of result is not a multiple of vector length (arg 2)”\n",
      "Warning message in cbind(parts$left, chars$ellip_h, parts$right, deparse.level = 0L):\n",
      "“number of rows of result is not a multiple of vector length (arg 2)”\n",
      "Warning message in cbind(parts$left, chars$ellip_h, parts$right, deparse.level = 0L):\n",
      "“number of rows of result is not a multiple of vector length (arg 2)”\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 0 × 22</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>module_id</th><th scope=col>eqtl_id</th><th scope=col>transcript_id</th><th scope=col>gene_id</th><th scope=col>gene_name</th><th scope=col>developmental_stage</th><th scope=col>tissue</th><th scope=col>eqtl_phenotype</th><th scope=col>eqtl_type</th><th scope=col>expressed_ipsc_ppc</th><th scope=col>⋯</th><th scope=col>LD_ipsc_ppc</th><th scope=col>LD_islets</th><th scope=col>LD_pancreas</th><th scope=col>LD_islets_conditional</th><th scope=col>LD_pancreas_conditional</th><th scope=col>islet_egene_overlap</th><th scope=col>pancreas_egene_overlap</th><th scope=col>module_pass</th><th scope=col>category_annotation</th><th scope=col>notes</th></tr>\n",
       "\t<tr><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;lgl&gt;</th><th scope=col>⋯</th><th scope=col>&lt;lgl&gt;</th><th scope=col>&lt;lgl&gt;</th><th scope=col>&lt;lgl&gt;</th><th scope=col>&lt;lgl&gt;</th><th scope=col>&lt;lgl&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;lgl&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 0 × 22\n",
       "\\begin{tabular}{lllllllllllllllllllll}\n",
       " module\\_id & eqtl\\_id & transcript\\_id & gene\\_id & gene\\_name & developmental\\_stage & tissue & eqtl\\_phenotype & eqtl\\_type & expressed\\_ipsc\\_ppc & ⋯ & LD\\_ipsc\\_ppc & LD\\_islets & LD\\_pancreas & LD\\_islets\\_conditional & LD\\_pancreas\\_conditional & islet\\_egene\\_overlap & pancreas\\_egene\\_overlap & module\\_pass & category\\_annotation & notes\\\\\n",
       " <chr> & <chr> & <chr> & <chr> & <chr> & <chr> & <chr> & <chr> & <chr> & <lgl> & ⋯ & <lgl> & <lgl> & <lgl> & <lgl> & <lgl> & <chr> & <chr> & <lgl> & <chr> & <chr>\\\\\n",
       "\\hline\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 0 × 22\n",
       "\n",
       "| module_id &lt;chr&gt; | eqtl_id &lt;chr&gt; | transcript_id &lt;chr&gt; | gene_id &lt;chr&gt; | gene_name &lt;chr&gt; | developmental_stage &lt;chr&gt; | tissue &lt;chr&gt; | eqtl_phenotype &lt;chr&gt; | eqtl_type &lt;chr&gt; | expressed_ipsc_ppc &lt;lgl&gt; | ⋯ ⋯ | LD_ipsc_ppc &lt;lgl&gt; | LD_islets &lt;lgl&gt; | LD_pancreas &lt;lgl&gt; | LD_islets_conditional &lt;lgl&gt; | LD_pancreas_conditional &lt;lgl&gt; | islet_egene_overlap &lt;chr&gt; | pancreas_egene_overlap &lt;chr&gt; | module_pass &lt;lgl&gt; | category_annotation &lt;chr&gt; | notes &lt;chr&gt; |\n",
       "|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n",
       "\n"
      ],
      "text/plain": [
       "     module_id eqtl_id transcript_id gene_id gene_name developmental_stage\n",
       "     tissue eqtl_phenotype eqtl_type expressed_ipsc_ppc ⋯ LD_ipsc_ppc LD_islets\n",
       "     LD_pancreas LD_islets_conditional LD_pancreas_conditional\n",
       "     islet_egene_overlap pancreas_egene_overlap module_pass category_annotation\n",
       "     notes"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 0 × 7</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>hg19_snp</th><th scope=col>rank</th><th scope=col>phenotype_id</th><th scope=col>chr</th><th scope=col>tissue</th><th scope=col>analysis</th><th scope=col>qtl_id</th></tr>\n",
       "\t<tr><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;int&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 0 × 7\n",
       "\\begin{tabular}{lllllll}\n",
       " hg19\\_snp & rank & phenotype\\_id & chr & tissue & analysis & qtl\\_id\\\\\n",
       " <chr> & <int> & <chr> & <chr> & <chr> & <chr> & <chr>\\\\\n",
       "\\hline\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 0 × 7\n",
       "\n",
       "| hg19_snp &lt;chr&gt; | rank &lt;int&gt; | phenotype_id &lt;chr&gt; | chr &lt;chr&gt; | tissue &lt;chr&gt; | analysis &lt;chr&gt; | qtl_id &lt;chr&gt; |\n",
       "|---|---|---|---|---|---|---|\n",
       "\n"
      ],
      "text/plain": [
       "     hg19_snp rank phenotype_id chr tissue analysis qtl_id"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 0 × 7</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>hg19_snp</th><th scope=col>rank</th><th scope=col>phenotype_id</th><th scope=col>chr</th><th scope=col>tissue</th><th scope=col>analysis</th><th scope=col>qtl_id</th></tr>\n",
       "\t<tr><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;int&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 0 × 7\n",
       "\\begin{tabular}{lllllll}\n",
       " hg19\\_snp & rank & phenotype\\_id & chr & tissue & analysis & qtl\\_id\\\\\n",
       " <chr> & <int> & <chr> & <chr> & <chr> & <chr> & <chr>\\\\\n",
       "\\hline\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 0 × 7\n",
       "\n",
       "| hg19_snp &lt;chr&gt; | rank &lt;int&gt; | phenotype_id &lt;chr&gt; | chr &lt;chr&gt; | tissue &lt;chr&gt; | analysis &lt;chr&gt; | qtl_id &lt;chr&gt; |\n",
       "|---|---|---|---|---|---|---|\n",
       "\n"
      ],
      "text/plain": [
       "     hg19_snp rank phenotype_id chr tissue analysis qtl_id"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 0 × 7</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>hg19_snp</th><th scope=col>rank</th><th scope=col>phenotype_id</th><th scope=col>chr</th><th scope=col>tissue</th><th scope=col>analysis</th><th scope=col>qtl_id</th></tr>\n",
       "\t<tr><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;int&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 0 × 7\n",
       "\\begin{tabular}{lllllll}\n",
       " hg19\\_snp & rank & phenotype\\_id & chr & tissue & analysis & qtl\\_id\\\\\n",
       " <chr> & <int> & <chr> & <chr> & <chr> & <chr> & <chr>\\\\\n",
       "\\hline\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 0 × 7\n",
       "\n",
       "| hg19_snp &lt;chr&gt; | rank &lt;int&gt; | phenotype_id &lt;chr&gt; | chr &lt;chr&gt; | tissue &lt;chr&gt; | analysis &lt;chr&gt; | qtl_id &lt;chr&gt; |\n",
       "|---|---|---|---|---|---|---|\n",
       "\n"
      ],
      "text/plain": [
       "     hg19_snp rank phenotype_id chr tissue analysis qtl_id"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 0 × 7</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>hg19_snp</th><th scope=col>rank</th><th scope=col>phenotype_id</th><th scope=col>chr</th><th scope=col>tissue</th><th scope=col>analysis</th><th scope=col>qtl_id</th></tr>\n",
       "\t<tr><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;int&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 0 × 7\n",
       "\\begin{tabular}{lllllll}\n",
       " hg19\\_snp & rank & phenotype\\_id & chr & tissue & analysis & qtl\\_id\\\\\n",
       " <chr> & <int> & <chr> & <chr> & <chr> & <chr> & <chr>\\\\\n",
       "\\hline\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 0 × 7\n",
       "\n",
       "| hg19_snp &lt;chr&gt; | rank &lt;int&gt; | phenotype_id &lt;chr&gt; | chr &lt;chr&gt; | tissue &lt;chr&gt; | analysis &lt;chr&gt; | qtl_id &lt;chr&gt; |\n",
       "|---|---|---|---|---|---|---|\n",
       "\n"
      ],
      "text/plain": [
       "     hg19_snp rank phenotype_id chr tissue analysis qtl_id"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# check that all eqtls have a testing snp\n",
    "eqtl_annot = fread(\"reviews/tables/Table_S11_eQTL_Annotations.txt\", data.table = F)\n",
    "eqtl_annot %>% filter(!eqtl_id %in% top$qtl_id)\n",
    "\n",
    "cond_list[[\"panc_eqtl\" ]] %>% filter(!qtl_id %in% top$qtl_id) %>% filter(chr != \"X\")\n",
    "cond_list[[\"panc_sqtl\" ]] %>% filter(!qtl_id %in% top$qtl_id) %>% filter(chr != \"X\")\n",
    "cond_list[[\"islet_eqtl\"]] %>% filter(!qtl_id %in% top$qtl_id) %>% filter(chr != \"23\")\n",
    "cond_list[[\"islet_sqtl\"]] %>% filter(!qtl_id %in% top$qtl_id) %>% filter(chr != \"23\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Save the testing snps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "   1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16 \n",
       "5306 3893 3202 2133 2703 3517 3358 1857 2293 2387 3208 2678  967 1868 1953 2717 \n",
       "  17   18   19   20   21   22 \n",
       "3247  766 4043 1388  800 1712 "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# number of snps to test per chromosome\n",
    "table(top$chrom)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Remove \"VAR_\" from snp id to match with VCF snp ids\n",
    "top$snp = gsub(\"VAR_\", \"\", top$snp)\n",
    "\n",
    "# un-factorise dataframe\n",
    "top = as.data.frame(top %>% ungroup())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 224,
   "metadata": {},
   "outputs": [],
   "source": [
    "# save\n",
    "fwrite(top, \"reviews/LD/r2/top.txt\", row.names = F, sep = \"\\t\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A grouped_df: 2 × 10</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>SNP.PP</th><th scope=col>snp</th><th scope=col>qtl_id</th><th scope=col>tissue</th><th scope=col>analysis</th><th scope=col>chrom</th><th scope=col>pos</th><th scope=col>type</th><th scope=col>transcript_id</th><th scope=col>in_1kg</th></tr>\n",
       "\t<tr><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;lgl&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><td>0.04718508</td><td>1_113061959_G_C</td><td>ipsc-ppc_0_ENSG00000007341</td><td>ipsc-ppc</td><td>gene</td><td>1</td><td>113061959</td><td>0</td><td>ENSG00000007341</td><td>TRUE</td></tr>\n",
       "\t<tr><td>0.10571673</td><td>1_1612540_C_T  </td><td>ipsc-ppc_0_ENSG00000008128</td><td>ipsc-ppc</td><td>gene</td><td>1</td><td>  1612540</td><td>0</td><td>ENSG00000008128</td><td>TRUE</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A grouped\\_df: 2 × 10\n",
       "\\begin{tabular}{llllllllll}\n",
       " SNP.PP & snp & qtl\\_id & tissue & analysis & chrom & pos & type & transcript\\_id & in\\_1kg\\\\\n",
       " <dbl> & <chr> & <chr> & <chr> & <chr> & <dbl> & <dbl> & <chr> & <chr> & <lgl>\\\\\n",
       "\\hline\n",
       "\t 0.04718508 & 1\\_113061959\\_G\\_C & ipsc-ppc\\_0\\_ENSG00000007341 & ipsc-ppc & gene & 1 & 113061959 & 0 & ENSG00000007341 & TRUE\\\\\n",
       "\t 0.10571673 & 1\\_1612540\\_C\\_T   & ipsc-ppc\\_0\\_ENSG00000008128 & ipsc-ppc & gene & 1 &   1612540 & 0 & ENSG00000008128 & TRUE\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A grouped_df: 2 × 10\n",
       "\n",
       "| SNP.PP &lt;dbl&gt; | snp &lt;chr&gt; | qtl_id &lt;chr&gt; | tissue &lt;chr&gt; | analysis &lt;chr&gt; | chrom &lt;dbl&gt; | pos &lt;dbl&gt; | type &lt;chr&gt; | transcript_id &lt;chr&gt; | in_1kg &lt;lgl&gt; |\n",
       "|---|---|---|---|---|---|---|---|---|---|\n",
       "| 0.04718508 | 1_113061959_G_C | ipsc-ppc_0_ENSG00000007341 | ipsc-ppc | gene | 1 | 113061959 | 0 | ENSG00000007341 | TRUE |\n",
       "| 0.10571673 | 1_1612540_C_T   | ipsc-ppc_0_ENSG00000008128 | ipsc-ppc | gene | 1 |   1612540 | 0 | ENSG00000008128 | TRUE |\n",
       "\n"
      ],
      "text/plain": [
       "  SNP.PP     snp             qtl_id                     tissue   analysis chrom\n",
       "1 0.04718508 1_113061959_G_C ipsc-ppc_0_ENSG00000007341 ipsc-ppc gene     1    \n",
       "2 0.10571673 1_1612540_C_T   ipsc-ppc_0_ENSG00000008128 ipsc-ppc gene     1    \n",
       "  pos       type transcript_id   in_1kg\n",
       "1 113061959 0    ENSG00000007341 TRUE  \n",
       "2   1612540 0    ENSG00000008128 TRUE  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "head(top,2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Run plink r2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "echo \"plink --memory 15000 --threads 4 --vcf ~/references/1kg/vcf/chr1.vcf.gz --extract reviews/LD/r2/input/chr1.txt --keep-allele-order --r2 square --make-bed --out reviews/LD/r2/out/chr1\" | qsub -N chr1 -pe smp 4 -V -cwd -o reviews/LD/r2/logs/chr1.out -e reviews/LD/r2/logs/chr1.err\n",
      "\n",
      "echo \"plink --memory 15000 --threads 4 --vcf ~/references/1kg/vcf/chr2.vcf.gz --extract reviews/LD/r2/input/chr2.txt --keep-allele-order --r2 square --make-bed --out reviews/LD/r2/out/chr2\" | qsub -N chr2 -pe smp 4 -V -cwd -o reviews/LD/r2/logs/chr2.out -e reviews/LD/r2/logs/chr2.err\n",
      "\n",
      "echo \"plink --memory 15000 --threads 4 --vcf ~/references/1kg/vcf/chr3.vcf.gz --extract reviews/LD/r2/input/chr3.txt --keep-allele-order --r2 square --make-bed --out reviews/LD/r2/out/chr3\" | qsub -N chr3 -pe smp 4 -V -cwd -o reviews/LD/r2/logs/chr3.out -e reviews/LD/r2/logs/chr3.err\n",
      "\n",
      "echo \"plink --memory 15000 --threads 4 --vcf ~/references/1kg/vcf/chr4.vcf.gz --extract reviews/LD/r2/input/chr4.txt --keep-allele-order --r2 square --make-bed --out reviews/LD/r2/out/chr4\" | qsub -N chr4 -pe smp 4 -V -cwd -o reviews/LD/r2/logs/chr4.out -e reviews/LD/r2/logs/chr4.err\n",
      "\n",
      "echo \"plink --memory 15000 --threads 4 --vcf ~/references/1kg/vcf/chr5.vcf.gz --extract reviews/LD/r2/input/chr5.txt --keep-allele-order --r2 square --make-bed --out reviews/LD/r2/out/chr5\" | qsub -N chr5 -pe smp 4 -V -cwd -o reviews/LD/r2/logs/chr5.out -e reviews/LD/r2/logs/chr5.err\n",
      "\n",
      "echo \"plink --memory 15000 --threads 4 --vcf ~/references/1kg/vcf/chr6.vcf.gz --extract reviews/LD/r2/input/chr6.txt --keep-allele-order --r2 square --make-bed --out reviews/LD/r2/out/chr6\" | qsub -N chr6 -pe smp 4 -V -cwd -o reviews/LD/r2/logs/chr6.out -e reviews/LD/r2/logs/chr6.err\n",
      "\n",
      "echo \"plink --memory 15000 --threads 4 --vcf ~/references/1kg/vcf/chr7.vcf.gz --extract reviews/LD/r2/input/chr7.txt --keep-allele-order --r2 square --make-bed --out reviews/LD/r2/out/chr7\" | qsub -N chr7 -pe smp 4 -V -cwd -o reviews/LD/r2/logs/chr7.out -e reviews/LD/r2/logs/chr7.err\n",
      "\n",
      "echo \"plink --memory 15000 --threads 4 --vcf ~/references/1kg/vcf/chr8.vcf.gz --extract reviews/LD/r2/input/chr8.txt --keep-allele-order --r2 square --make-bed --out reviews/LD/r2/out/chr8\" | qsub -N chr8 -pe smp 4 -V -cwd -o reviews/LD/r2/logs/chr8.out -e reviews/LD/r2/logs/chr8.err\n",
      "\n",
      "echo \"plink --memory 15000 --threads 4 --vcf ~/references/1kg/vcf/chr9.vcf.gz --extract reviews/LD/r2/input/chr9.txt --keep-allele-order --r2 square --make-bed --out reviews/LD/r2/out/chr9\" | qsub -N chr9 -pe smp 4 -V -cwd -o reviews/LD/r2/logs/chr9.out -e reviews/LD/r2/logs/chr9.err\n",
      "\n",
      "echo \"plink --memory 15000 --threads 4 --vcf ~/references/1kg/vcf/chr10.vcf.gz --extract reviews/LD/r2/input/chr10.txt --keep-allele-order --r2 square --make-bed --out reviews/LD/r2/out/chr10\" | qsub -N chr10 -pe smp 4 -V -cwd -o reviews/LD/r2/logs/chr10.out -e reviews/LD/r2/logs/chr10.err\n",
      "\n",
      "echo \"plink --memory 15000 --threads 4 --vcf ~/references/1kg/vcf/chr11.vcf.gz --extract reviews/LD/r2/input/chr11.txt --keep-allele-order --r2 square --make-bed --out reviews/LD/r2/out/chr11\" | qsub -N chr11 -pe smp 4 -V -cwd -o reviews/LD/r2/logs/chr11.out -e reviews/LD/r2/logs/chr11.err\n",
      "\n",
      "echo \"plink --memory 15000 --threads 4 --vcf ~/references/1kg/vcf/chr12.vcf.gz --extract reviews/LD/r2/input/chr12.txt --keep-allele-order --r2 square --make-bed --out reviews/LD/r2/out/chr12\" | qsub -N chr12 -pe smp 4 -V -cwd -o reviews/LD/r2/logs/chr12.out -e reviews/LD/r2/logs/chr12.err\n",
      "\n",
      "echo \"plink --memory 15000 --threads 4 --vcf ~/references/1kg/vcf/chr13.vcf.gz --extract reviews/LD/r2/input/chr13.txt --keep-allele-order --r2 square --make-bed --out reviews/LD/r2/out/chr13\" | qsub -N chr13 -pe smp 4 -V -cwd -o reviews/LD/r2/logs/chr13.out -e reviews/LD/r2/logs/chr13.err\n",
      "\n",
      "echo \"plink --memory 15000 --threads 4 --vcf ~/references/1kg/vcf/chr14.vcf.gz --extract reviews/LD/r2/input/chr14.txt --keep-allele-order --r2 square --make-bed --out reviews/LD/r2/out/chr14\" | qsub -N chr14 -pe smp 4 -V -cwd -o reviews/LD/r2/logs/chr14.out -e reviews/LD/r2/logs/chr14.err\n",
      "\n",
      "echo \"plink --memory 15000 --threads 4 --vcf ~/references/1kg/vcf/chr15.vcf.gz --extract reviews/LD/r2/input/chr15.txt --keep-allele-order --r2 square --make-bed --out reviews/LD/r2/out/chr15\" | qsub -N chr15 -pe smp 4 -V -cwd -o reviews/LD/r2/logs/chr15.out -e reviews/LD/r2/logs/chr15.err\n",
      "\n",
      "echo \"plink --memory 15000 --threads 4 --vcf ~/references/1kg/vcf/chr16.vcf.gz --extract reviews/LD/r2/input/chr16.txt --keep-allele-order --r2 square --make-bed --out reviews/LD/r2/out/chr16\" | qsub -N chr16 -pe smp 4 -V -cwd -o reviews/LD/r2/logs/chr16.out -e reviews/LD/r2/logs/chr16.err\n",
      "\n",
      "echo \"plink --memory 15000 --threads 4 --vcf ~/references/1kg/vcf/chr17.vcf.gz --extract reviews/LD/r2/input/chr17.txt --keep-allele-order --r2 square --make-bed --out reviews/LD/r2/out/chr17\" | qsub -N chr17 -pe smp 4 -V -cwd -o reviews/LD/r2/logs/chr17.out -e reviews/LD/r2/logs/chr17.err\n",
      "\n",
      "echo \"plink --memory 15000 --threads 4 --vcf ~/references/1kg/vcf/chr18.vcf.gz --extract reviews/LD/r2/input/chr18.txt --keep-allele-order --r2 square --make-bed --out reviews/LD/r2/out/chr18\" | qsub -N chr18 -pe smp 4 -V -cwd -o reviews/LD/r2/logs/chr18.out -e reviews/LD/r2/logs/chr18.err\n",
      "\n",
      "echo \"plink --memory 15000 --threads 4 --vcf ~/references/1kg/vcf/chr19.vcf.gz --extract reviews/LD/r2/input/chr19.txt --keep-allele-order --r2 square --make-bed --out reviews/LD/r2/out/chr19\" | qsub -N chr19 -pe smp 4 -V -cwd -o reviews/LD/r2/logs/chr19.out -e reviews/LD/r2/logs/chr19.err\n",
      "\n",
      "echo \"plink --memory 15000 --threads 4 --vcf ~/references/1kg/vcf/chr20.vcf.gz --extract reviews/LD/r2/input/chr20.txt --keep-allele-order --r2 square --make-bed --out reviews/LD/r2/out/chr20\" | qsub -N chr20 -pe smp 4 -V -cwd -o reviews/LD/r2/logs/chr20.out -e reviews/LD/r2/logs/chr20.err\n",
      "\n",
      "echo \"plink --memory 15000 --threads 4 --vcf ~/references/1kg/vcf/chr21.vcf.gz --extract reviews/LD/r2/input/chr21.txt --keep-allele-order --r2 square --make-bed --out reviews/LD/r2/out/chr21\" | qsub -N chr21 -pe smp 4 -V -cwd -o reviews/LD/r2/logs/chr21.out -e reviews/LD/r2/logs/chr21.err\n",
      "\n",
      "echo \"plink --memory 15000 --threads 4 --vcf ~/references/1kg/vcf/chr22.vcf.gz --extract reviews/LD/r2/input/chr22.txt --keep-allele-order --r2 square --make-bed --out reviews/LD/r2/out/chr22\" | qsub -N chr22 -pe smp 4 -V -cwd -o reviews/LD/r2/logs/chr22.out -e reviews/LD/r2/logs/chr22.err\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# run plink on qsub\n",
    "\n",
    "for (c in c(1:22))\n",
    "{\n",
    "    outfile = paste(\"reviews/LD/r2/out\", paste0(\"chr\", c), sep = \"/\") # after reviews\n",
    "    snpfile = paste(\"reviews/LD/r2/input\", paste0(\"chr\", c, \".txt\"), sep = \"/\") # after reviews\n",
    "    logout = paste(\"reviews/LD/r2/logs\", paste(paste0(\"chr\", c), \"out\", sep = \".\"), sep = \"/\")\n",
    "    logerr = paste(\"reviews/LD/r2/logs\", paste(paste0(\"chr\", c), \"err\", sep = \".\"), sep = \"/\")\n",
    "    \n",
    "    writeLines(unique(top[top$chrom == c,]$snp), snpfile, sep = \"\\n\")\n",
    "    \n",
    "    cmd = paste(\"plink\", \"--memory 15000 --threads 4\", \"--vcf\", paste0(\"~/references/1kg/vcf/\", paste0(\"chr\", c), \".vcf.gz\"), \n",
    "          \"--extract\", snpfile, \"--keep-allele-order\", \"--r2\", \"square\", \"--make-bed\", \"--out\", outfile)\n",
    "\n",
    "    qsub_cmd = paste(\"echo\", paste0(\"\\\"\", cmd, \"\\\"\" ), \"|\", \"qsub -N\", paste0(\"chr\", c),\n",
    "                     \"-pe smp 4\", \"-V -cwd\", \n",
    "                     \"-o\", log_out,\n",
    "                     \"-e\", log_err)\n",
    "    message(qsub_cmd)\n",
    "    system(qsub_cmd)\n",
    "}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Process results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 423,
   "metadata": {},
   "outputs": [],
   "source": [
    "top = fread(\"reviews/LD/r2/top.txt\", data.table = F)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 829,
   "metadata": {},
   "outputs": [],
   "source": [
    "eqtl_annot = fread(\"reviews/tables/Table_S11_eQTL_Annotations.txt\", data.table = F) \n",
    "\n",
    "# merge testing snp with their eqtls\n",
    "eqtl_annot = merge(eqtl_annot, top %>% select(chrom, qtl_id) %>% dplyr::rename(eqtl_id = qtl_id), all.x = T)\n",
    "\n",
    "# add module_id column - if singleton, use the eqtl_id, if not, use the module_id\n",
    "eqtl_annot$module_id = ifelse(eqtl_annot$eqtl_type == \"singleton\", eqtl_annot$eqtl_id, eqtl_annot$module_id)\n",
    "\n",
    "# annotation whether gene or isoform (i.e., alternative splicing)\n",
    "eqtl_annot$analysis = ifelse(eqtl_annot$eqtl_phenotype %like% \"gene\", \"gene\", \"isoform\")\n",
    "\n",
    "# give taskid number for qsub purposes\n",
    "eqtl_annot$taskid = c(1:nrow(eqtl_annot))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    - run on qsub projects/PPC/analysis/ppc_eqtls/reviews/process_LD_r2.R"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### test script on one eqtl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1057,
   "metadata": {},
   "outputs": [],
   "source": [
    "row = 1041"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1069,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "qtl_id: ipsc-ppc_0_ENSG00000128311\n",
      "\n",
      "qtltype: singleton\n",
      "\n",
      "transcript: ENSG00000128311\n",
      "\n",
      "phenotype: gene\n",
      "\n",
      "snp: 22_37405408_G_T\n",
      "\n",
      "chromosome: 22\n",
      "\n",
      "Do not test these qtls:\n",
      "ipsc-ppc_0_ENSG00000128311\n",
      "\n",
      "Testing these QTLs: ================\n",
      "\n",
      "Opening LD\n",
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     Min.   1st Qu.    Median      Mean   3rd Qu.      Max. \n",
      "3.930e-06 4.542e-04 2.491e-03 1.091e-02 7.705e-03 9.179e-02 \n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "          \n",
       "           FALSE\n",
       "  ipsc-ppc     3\n",
       "  islet        8\n",
       "  pancreas    18"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Annotated for LD\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 29 × 14</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>SNP.PP</th><th scope=col>snp</th><th scope=col>qtl_id</th><th scope=col>tissue</th><th scope=col>analysis</th><th scope=col>chrom</th><th scope=col>pos</th><th scope=col>type</th><th scope=col>transcript_id</th><th scope=col>in_1kg</th><th scope=col>r2</th><th scope=col>in_r2</th><th scope=col>in_window</th><th scope=col>in_ld</th></tr>\n",
       "\t<tr><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;int&gt;</th><th scope=col>&lt;int&gt;</th><th scope=col>&lt;int&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;lgl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;lgl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;lgl&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><td>0.07657956</td><td>22_36905776_G_A</td><td>pancreas_0_ENSG00000100350</td><td>pancreas</td><td>gene</td><td>22</td><td>36905776</td><td>0</td><td>ENSG00000100350</td><td>TRUE</td><td>5.23631e-02</td><td>FALSE</td><td>499632</td><td>FALSE</td></tr>\n",
       "\t<tr><td>0.05335938</td><td>22_36914347_C_G</td><td>islet_0_ENSG00000100348   </td><td>islet   </td><td>gene</td><td>22</td><td>36914347</td><td>0</td><td>ENSG00000100348</td><td>TRUE</td><td>3.93315e-06</td><td>FALSE</td><td>491061</td><td>FALSE</td></tr>\n",
       "\t<tr><td>0.01000000</td><td>22_36914347_C_G</td><td>islet_1_ENSG00000100348   </td><td>islet   </td><td>gene</td><td>22</td><td>36914347</td><td>1</td><td>ENSG00000100348</td><td>TRUE</td><td>3.93315e-06</td><td>FALSE</td><td>491061</td><td>FALSE</td></tr>\n",
       "\t<tr><td>0.01000000</td><td>22_37008928_A_G</td><td>pancreas_2_ENSG00000100360</td><td>pancreas</td><td>gene</td><td>22</td><td>37008928</td><td>2</td><td>ENSG00000100360</td><td>TRUE</td><td>7.11913e-06</td><td>FALSE</td><td>396480</td><td>FALSE</td></tr>\n",
       "\t<tr><td>0.29685296</td><td>22_37798644_G_T</td><td>islet_0_ENSG00000166897   </td><td>islet   </td><td>gene</td><td>22</td><td>37798644</td><td>0</td><td>ENSG00000166897</td><td>TRUE</td><td>1.07757e-03</td><td>FALSE</td><td>393236</td><td>FALSE</td></tr>\n",
       "\t<tr><td>0.01000000</td><td>22_37798644_G_T</td><td>islet_1_ENSG00000166897   </td><td>islet   </td><td>gene</td><td>22</td><td>37798644</td><td>1</td><td>ENSG00000166897</td><td>TRUE</td><td>1.07757e-03</td><td>FALSE</td><td>393236</td><td>FALSE</td></tr>\n",
       "\t<tr><td>0.09109036</td><td>22_37797138_A_G</td><td>pancreas_0_ENSG00000166897</td><td>pancreas</td><td>gene</td><td>22</td><td>37797138</td><td>0</td><td>ENSG00000166897</td><td>TRUE</td><td>1.45221e-04</td><td>FALSE</td><td>391730</td><td>FALSE</td></tr>\n",
       "\t<tr><td>0.01000000</td><td>22_37797138_A_G</td><td>pancreas_1_ENSG00000166897</td><td>pancreas</td><td>gene</td><td>22</td><td>37797138</td><td>1</td><td>ENSG00000166897</td><td>TRUE</td><td>1.45221e-04</td><td>FALSE</td><td>391730</td><td>FALSE</td></tr>\n",
       "\t<tr><td>0.04098229</td><td>22_37727452_C_G</td><td>ipsc-ppc_0_ENSG00000189060</td><td>ipsc-ppc</td><td>gene</td><td>22</td><td>37727452</td><td>0</td><td>ENSG00000189060</td><td>TRUE</td><td>3.64086e-03</td><td>FALSE</td><td>322044</td><td>FALSE</td></tr>\n",
       "\t<tr><td>0.47984539</td><td>22_37722890_A_G</td><td>ipsc-ppc_0_ENSG00000166897</td><td>ipsc-ppc</td><td>gene</td><td>22</td><td>37722890</td><td>0</td><td>ENSG00000166897</td><td>TRUE</td><td>2.09514e-03</td><td>FALSE</td><td>317482</td><td>FALSE</td></tr>\n",
       "\t<tr><td>0.04123813</td><td>22_37682496_G_C</td><td>pancreas_0_ENSG00000100055</td><td>pancreas</td><td>gene</td><td>22</td><td>37682496</td><td>0</td><td>ENSG00000100055</td><td>TRUE</td><td>9.20931e-04</td><td>FALSE</td><td>277088</td><td>FALSE</td></tr>\n",
       "\t<tr><td>0.01000000</td><td>22_37682496_G_C</td><td>pancreas_1_ENSG00000100055</td><td>pancreas</td><td>gene</td><td>22</td><td>37682496</td><td>1</td><td>ENSG00000100055</td><td>TRUE</td><td>9.20931e-04</td><td>FALSE</td><td>277088</td><td>FALSE</td></tr>\n",
       "\t<tr><td>0.18274071</td><td>22_37170977_C_T</td><td>pancreas_0_ENSG00000100360</td><td>pancreas</td><td>gene</td><td>22</td><td>37170977</td><td>0</td><td>ENSG00000100360</td><td>TRUE</td><td>1.72029e-02</td><td>FALSE</td><td>234431</td><td>FALSE</td></tr>\n",
       "\t<tr><td>0.01000000</td><td>22_37170977_C_T</td><td>pancreas_1_ENSG00000100360</td><td>pancreas</td><td>gene</td><td>22</td><td>37170977</td><td>1</td><td>ENSG00000100360</td><td>TRUE</td><td>1.72029e-02</td><td>FALSE</td><td>234431</td><td>FALSE</td></tr>\n",
       "\t<tr><td>0.16038611</td><td>22_37171611_A_G</td><td>ipsc-ppc_0_ENSG00000100360</td><td>ipsc-ppc</td><td>gene</td><td>22</td><td>37171611</td><td>0</td><td>ENSG00000100360</td><td>TRUE</td><td>3.71433e-03</td><td>FALSE</td><td>233797</td><td>FALSE</td></tr>\n",
       "\t<tr><td>0.01000000</td><td>22_37638046_T_C</td><td>pancreas_1_ENSG00000128309</td><td>pancreas</td><td>gene</td><td>22</td><td>37638046</td><td>1</td><td>ENSG00000128309</td><td>TRUE</td><td>4.71481e-04</td><td>FALSE</td><td>232638</td><td>FALSE</td></tr>\n",
       "\t<tr><td>0.01000000</td><td>22_37588820_A_G</td><td>pancreas_2_ENSG00000133466</td><td>pancreas</td><td>gene</td><td>22</td><td>37588820</td><td>2</td><td>ENSG00000133466</td><td>TRUE</td><td>4.36880e-04</td><td>FALSE</td><td>183412</td><td>FALSE</td></tr>\n",
       "\t<tr><td>0.12731895</td><td>22_37588013_T_C</td><td>pancreas_0_ENSG00000133466</td><td>pancreas</td><td>gene</td><td>22</td><td>37588013</td><td>0</td><td>ENSG00000133466</td><td>TRUE</td><td>1.09947e-02</td><td>FALSE</td><td>182605</td><td>FALSE</td></tr>\n",
       "\t<tr><td>0.01000000</td><td>22_37588013_T_C</td><td>pancreas_1_ENSG00000133466</td><td>pancreas</td><td>gene</td><td>22</td><td>37588013</td><td>1</td><td>ENSG00000133466</td><td>TRUE</td><td>1.09947e-02</td><td>FALSE</td><td>182605</td><td>FALSE</td></tr>\n",
       "\t<tr><td>0.39482843</td><td>22_37586792_C_T</td><td>islet_0_ENSG00000133466   </td><td>islet   </td><td>gene</td><td>22</td><td>37586792</td><td>0</td><td>ENSG00000133466</td><td>TRUE</td><td>1.22057e-02</td><td>FALSE</td><td>181384</td><td>FALSE</td></tr>\n",
       "\t<tr><td>0.01000000</td><td>22_37586792_C_T</td><td>islet_1_ENSG00000133466   </td><td>islet   </td><td>gene</td><td>22</td><td>37586792</td><td>1</td><td>ENSG00000133466</td><td>TRUE</td><td>1.22057e-02</td><td>FALSE</td><td>181384</td><td>FALSE</td></tr>\n",
       "\t<tr><td>0.01000000</td><td>22_37575695_A_G</td><td>pancreas_1_ENSG00000189060</td><td>pancreas</td><td>gene</td><td>22</td><td>37575695</td><td>1</td><td>ENSG00000189060</td><td>TRUE</td><td>4.41465e-03</td><td>FALSE</td><td>170287</td><td>FALSE</td></tr>\n",
       "\t<tr><td>0.04858043</td><td>22_37271013_G_C</td><td>pancreas_0_ENSG00000100368</td><td>pancreas</td><td>gene</td><td>22</td><td>37271013</td><td>0</td><td>ENSG00000100368</td><td>TRUE</td><td>3.18595e-03</td><td>FALSE</td><td>134395</td><td>FALSE</td></tr>\n",
       "\t<tr><td>0.01000000</td><td>22_37271013_G_C</td><td>pancreas_1_ENSG00000100368</td><td>pancreas</td><td>gene</td><td>22</td><td>37271013</td><td>1</td><td>ENSG00000100368</td><td>TRUE</td><td>3.18595e-03</td><td>FALSE</td><td>134395</td><td>FALSE</td></tr>\n",
       "\t<tr><td>0.20768965</td><td>22_37276250_T_A</td><td>islet_0_ENSG00000100368   </td><td>islet   </td><td>gene</td><td>22</td><td>37276250</td><td>0</td><td>ENSG00000100368</td><td>TRUE</td><td>1.71799e-04</td><td>FALSE</td><td>129158</td><td>FALSE</td></tr>\n",
       "\t<tr><td>0.01000000</td><td>22_37276250_T_A</td><td>islet_1_ENSG00000100368   </td><td>islet   </td><td>gene</td><td>22</td><td>37276250</td><td>1</td><td>ENSG00000100368</td><td>TRUE</td><td>1.71799e-04</td><td>FALSE</td><td>129158</td><td>FALSE</td></tr>\n",
       "\t<tr><td>0.01000000</td><td>22_37421702_A_G</td><td>pancreas_2_ENSG00000100350</td><td>pancreas</td><td>gene</td><td>22</td><td>37421702</td><td>2</td><td>ENSG00000100350</td><td>TRUE</td><td>2.49063e-03</td><td>FALSE</td><td> 16294</td><td>FALSE</td></tr>\n",
       "\t<tr><td>0.05709081</td><td>22_37400957_G_A</td><td>pancreas_0_ENSG00000128311</td><td>pancreas</td><td>gene</td><td>22</td><td>37400957</td><td>0</td><td>ENSG00000128311</td><td>TRUE</td><td>9.17943e-02</td><td>FALSE</td><td>  4451</td><td>FALSE</td></tr>\n",
       "\t<tr><td>0.01000000</td><td>22_37400957_G_A</td><td>pancreas_1_ENSG00000128311</td><td>pancreas</td><td>gene</td><td>22</td><td>37400957</td><td>1</td><td>ENSG00000128311</td><td>TRUE</td><td>9.17943e-02</td><td>FALSE</td><td>  4451</td><td>FALSE</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 29 × 14\n",
       "\\begin{tabular}{llllllllllllll}\n",
       " SNP.PP & snp & qtl\\_id & tissue & analysis & chrom & pos & type & transcript\\_id & in\\_1kg & r2 & in\\_r2 & in\\_window & in\\_ld\\\\\n",
       " <dbl> & <chr> & <chr> & <chr> & <chr> & <int> & <int> & <int> & <chr> & <lgl> & <dbl> & <lgl> & <dbl> & <lgl>\\\\\n",
       "\\hline\n",
       "\t 0.07657956 & 22\\_36905776\\_G\\_A & pancreas\\_0\\_ENSG00000100350 & pancreas & gene & 22 & 36905776 & 0 & ENSG00000100350 & TRUE & 5.23631e-02 & FALSE & 499632 & FALSE\\\\\n",
       "\t 0.05335938 & 22\\_36914347\\_C\\_G & islet\\_0\\_ENSG00000100348    & islet    & gene & 22 & 36914347 & 0 & ENSG00000100348 & TRUE & 3.93315e-06 & FALSE & 491061 & FALSE\\\\\n",
       "\t 0.01000000 & 22\\_36914347\\_C\\_G & islet\\_1\\_ENSG00000100348    & islet    & gene & 22 & 36914347 & 1 & ENSG00000100348 & TRUE & 3.93315e-06 & FALSE & 491061 & FALSE\\\\\n",
       "\t 0.01000000 & 22\\_37008928\\_A\\_G & pancreas\\_2\\_ENSG00000100360 & pancreas & gene & 22 & 37008928 & 2 & ENSG00000100360 & TRUE & 7.11913e-06 & FALSE & 396480 & FALSE\\\\\n",
       "\t 0.29685296 & 22\\_37798644\\_G\\_T & islet\\_0\\_ENSG00000166897    & islet    & gene & 22 & 37798644 & 0 & ENSG00000166897 & TRUE & 1.07757e-03 & FALSE & 393236 & FALSE\\\\\n",
       "\t 0.01000000 & 22\\_37798644\\_G\\_T & islet\\_1\\_ENSG00000166897    & islet    & gene & 22 & 37798644 & 1 & ENSG00000166897 & TRUE & 1.07757e-03 & FALSE & 393236 & FALSE\\\\\n",
       "\t 0.09109036 & 22\\_37797138\\_A\\_G & pancreas\\_0\\_ENSG00000166897 & pancreas & gene & 22 & 37797138 & 0 & ENSG00000166897 & TRUE & 1.45221e-04 & FALSE & 391730 & FALSE\\\\\n",
       "\t 0.01000000 & 22\\_37797138\\_A\\_G & pancreas\\_1\\_ENSG00000166897 & pancreas & gene & 22 & 37797138 & 1 & ENSG00000166897 & TRUE & 1.45221e-04 & FALSE & 391730 & FALSE\\\\\n",
       "\t 0.04098229 & 22\\_37727452\\_C\\_G & ipsc-ppc\\_0\\_ENSG00000189060 & ipsc-ppc & gene & 22 & 37727452 & 0 & ENSG00000189060 & TRUE & 3.64086e-03 & FALSE & 322044 & FALSE\\\\\n",
       "\t 0.47984539 & 22\\_37722890\\_A\\_G & ipsc-ppc\\_0\\_ENSG00000166897 & ipsc-ppc & gene & 22 & 37722890 & 0 & ENSG00000166897 & TRUE & 2.09514e-03 & FALSE & 317482 & FALSE\\\\\n",
       "\t 0.04123813 & 22\\_37682496\\_G\\_C & pancreas\\_0\\_ENSG00000100055 & pancreas & gene & 22 & 37682496 & 0 & ENSG00000100055 & TRUE & 9.20931e-04 & FALSE & 277088 & FALSE\\\\\n",
       "\t 0.01000000 & 22\\_37682496\\_G\\_C & pancreas\\_1\\_ENSG00000100055 & pancreas & gene & 22 & 37682496 & 1 & ENSG00000100055 & TRUE & 9.20931e-04 & FALSE & 277088 & FALSE\\\\\n",
       "\t 0.18274071 & 22\\_37170977\\_C\\_T & pancreas\\_0\\_ENSG00000100360 & pancreas & gene & 22 & 37170977 & 0 & ENSG00000100360 & TRUE & 1.72029e-02 & FALSE & 234431 & FALSE\\\\\n",
       "\t 0.01000000 & 22\\_37170977\\_C\\_T & pancreas\\_1\\_ENSG00000100360 & pancreas & gene & 22 & 37170977 & 1 & ENSG00000100360 & TRUE & 1.72029e-02 & FALSE & 234431 & FALSE\\\\\n",
       "\t 0.16038611 & 22\\_37171611\\_A\\_G & ipsc-ppc\\_0\\_ENSG00000100360 & ipsc-ppc & gene & 22 & 37171611 & 0 & ENSG00000100360 & TRUE & 3.71433e-03 & FALSE & 233797 & FALSE\\\\\n",
       "\t 0.01000000 & 22\\_37638046\\_T\\_C & pancreas\\_1\\_ENSG00000128309 & pancreas & gene & 22 & 37638046 & 1 & ENSG00000128309 & TRUE & 4.71481e-04 & FALSE & 232638 & FALSE\\\\\n",
       "\t 0.01000000 & 22\\_37588820\\_A\\_G & pancreas\\_2\\_ENSG00000133466 & pancreas & gene & 22 & 37588820 & 2 & ENSG00000133466 & TRUE & 4.36880e-04 & FALSE & 183412 & FALSE\\\\\n",
       "\t 0.12731895 & 22\\_37588013\\_T\\_C & pancreas\\_0\\_ENSG00000133466 & pancreas & gene & 22 & 37588013 & 0 & ENSG00000133466 & TRUE & 1.09947e-02 & FALSE & 182605 & FALSE\\\\\n",
       "\t 0.01000000 & 22\\_37588013\\_T\\_C & pancreas\\_1\\_ENSG00000133466 & pancreas & gene & 22 & 37588013 & 1 & ENSG00000133466 & TRUE & 1.09947e-02 & FALSE & 182605 & FALSE\\\\\n",
       "\t 0.39482843 & 22\\_37586792\\_C\\_T & islet\\_0\\_ENSG00000133466    & islet    & gene & 22 & 37586792 & 0 & ENSG00000133466 & TRUE & 1.22057e-02 & FALSE & 181384 & FALSE\\\\\n",
       "\t 0.01000000 & 22\\_37586792\\_C\\_T & islet\\_1\\_ENSG00000133466    & islet    & gene & 22 & 37586792 & 1 & ENSG00000133466 & TRUE & 1.22057e-02 & FALSE & 181384 & FALSE\\\\\n",
       "\t 0.01000000 & 22\\_37575695\\_A\\_G & pancreas\\_1\\_ENSG00000189060 & pancreas & gene & 22 & 37575695 & 1 & ENSG00000189060 & TRUE & 4.41465e-03 & FALSE & 170287 & FALSE\\\\\n",
       "\t 0.04858043 & 22\\_37271013\\_G\\_C & pancreas\\_0\\_ENSG00000100368 & pancreas & gene & 22 & 37271013 & 0 & ENSG00000100368 & TRUE & 3.18595e-03 & FALSE & 134395 & FALSE\\\\\n",
       "\t 0.01000000 & 22\\_37271013\\_G\\_C & pancreas\\_1\\_ENSG00000100368 & pancreas & gene & 22 & 37271013 & 1 & ENSG00000100368 & TRUE & 3.18595e-03 & FALSE & 134395 & FALSE\\\\\n",
       "\t 0.20768965 & 22\\_37276250\\_T\\_A & islet\\_0\\_ENSG00000100368    & islet    & gene & 22 & 37276250 & 0 & ENSG00000100368 & TRUE & 1.71799e-04 & FALSE & 129158 & FALSE\\\\\n",
       "\t 0.01000000 & 22\\_37276250\\_T\\_A & islet\\_1\\_ENSG00000100368    & islet    & gene & 22 & 37276250 & 1 & ENSG00000100368 & TRUE & 1.71799e-04 & FALSE & 129158 & FALSE\\\\\n",
       "\t 0.01000000 & 22\\_37421702\\_A\\_G & pancreas\\_2\\_ENSG00000100350 & pancreas & gene & 22 & 37421702 & 2 & ENSG00000100350 & TRUE & 2.49063e-03 & FALSE &  16294 & FALSE\\\\\n",
       "\t 0.05709081 & 22\\_37400957\\_G\\_A & pancreas\\_0\\_ENSG00000128311 & pancreas & gene & 22 & 37400957 & 0 & ENSG00000128311 & TRUE & 9.17943e-02 & FALSE &   4451 & FALSE\\\\\n",
       "\t 0.01000000 & 22\\_37400957\\_G\\_A & pancreas\\_1\\_ENSG00000128311 & pancreas & gene & 22 & 37400957 & 1 & ENSG00000128311 & TRUE & 9.17943e-02 & FALSE &   4451 & FALSE\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 29 × 14\n",
       "\n",
       "| SNP.PP &lt;dbl&gt; | snp &lt;chr&gt; | qtl_id &lt;chr&gt; | tissue &lt;chr&gt; | analysis &lt;chr&gt; | chrom &lt;int&gt; | pos &lt;int&gt; | type &lt;int&gt; | transcript_id &lt;chr&gt; | in_1kg &lt;lgl&gt; | r2 &lt;dbl&gt; | in_r2 &lt;lgl&gt; | in_window &lt;dbl&gt; | in_ld &lt;lgl&gt; |\n",
       "|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n",
       "| 0.07657956 | 22_36905776_G_A | pancreas_0_ENSG00000100350 | pancreas | gene | 22 | 36905776 | 0 | ENSG00000100350 | TRUE | 5.23631e-02 | FALSE | 499632 | FALSE |\n",
       "| 0.05335938 | 22_36914347_C_G | islet_0_ENSG00000100348    | islet    | gene | 22 | 36914347 | 0 | ENSG00000100348 | TRUE | 3.93315e-06 | FALSE | 491061 | FALSE |\n",
       "| 0.01000000 | 22_36914347_C_G | islet_1_ENSG00000100348    | islet    | gene | 22 | 36914347 | 1 | ENSG00000100348 | TRUE | 3.93315e-06 | FALSE | 491061 | FALSE |\n",
       "| 0.01000000 | 22_37008928_A_G | pancreas_2_ENSG00000100360 | pancreas | gene | 22 | 37008928 | 2 | ENSG00000100360 | TRUE | 7.11913e-06 | FALSE | 396480 | FALSE |\n",
       "| 0.29685296 | 22_37798644_G_T | islet_0_ENSG00000166897    | islet    | gene | 22 | 37798644 | 0 | ENSG00000166897 | TRUE | 1.07757e-03 | FALSE | 393236 | FALSE |\n",
       "| 0.01000000 | 22_37798644_G_T | islet_1_ENSG00000166897    | islet    | gene | 22 | 37798644 | 1 | ENSG00000166897 | TRUE | 1.07757e-03 | FALSE | 393236 | FALSE |\n",
       "| 0.09109036 | 22_37797138_A_G | pancreas_0_ENSG00000166897 | pancreas | gene | 22 | 37797138 | 0 | ENSG00000166897 | TRUE | 1.45221e-04 | FALSE | 391730 | FALSE |\n",
       "| 0.01000000 | 22_37797138_A_G | pancreas_1_ENSG00000166897 | pancreas | gene | 22 | 37797138 | 1 | ENSG00000166897 | TRUE | 1.45221e-04 | FALSE | 391730 | FALSE |\n",
       "| 0.04098229 | 22_37727452_C_G | ipsc-ppc_0_ENSG00000189060 | ipsc-ppc | gene | 22 | 37727452 | 0 | ENSG00000189060 | TRUE | 3.64086e-03 | FALSE | 322044 | FALSE |\n",
       "| 0.47984539 | 22_37722890_A_G | ipsc-ppc_0_ENSG00000166897 | ipsc-ppc | gene | 22 | 37722890 | 0 | ENSG00000166897 | TRUE | 2.09514e-03 | FALSE | 317482 | FALSE |\n",
       "| 0.04123813 | 22_37682496_G_C | pancreas_0_ENSG00000100055 | pancreas | gene | 22 | 37682496 | 0 | ENSG00000100055 | TRUE | 9.20931e-04 | FALSE | 277088 | FALSE |\n",
       "| 0.01000000 | 22_37682496_G_C | pancreas_1_ENSG00000100055 | pancreas | gene | 22 | 37682496 | 1 | ENSG00000100055 | TRUE | 9.20931e-04 | FALSE | 277088 | FALSE |\n",
       "| 0.18274071 | 22_37170977_C_T | pancreas_0_ENSG00000100360 | pancreas | gene | 22 | 37170977 | 0 | ENSG00000100360 | TRUE | 1.72029e-02 | FALSE | 234431 | FALSE |\n",
       "| 0.01000000 | 22_37170977_C_T | pancreas_1_ENSG00000100360 | pancreas | gene | 22 | 37170977 | 1 | ENSG00000100360 | TRUE | 1.72029e-02 | FALSE | 234431 | FALSE |\n",
       "| 0.16038611 | 22_37171611_A_G | ipsc-ppc_0_ENSG00000100360 | ipsc-ppc | gene | 22 | 37171611 | 0 | ENSG00000100360 | TRUE | 3.71433e-03 | FALSE | 233797 | FALSE |\n",
       "| 0.01000000 | 22_37638046_T_C | pancreas_1_ENSG00000128309 | pancreas | gene | 22 | 37638046 | 1 | ENSG00000128309 | TRUE | 4.71481e-04 | FALSE | 232638 | FALSE |\n",
       "| 0.01000000 | 22_37588820_A_G | pancreas_2_ENSG00000133466 | pancreas | gene | 22 | 37588820 | 2 | ENSG00000133466 | TRUE | 4.36880e-04 | FALSE | 183412 | FALSE |\n",
       "| 0.12731895 | 22_37588013_T_C | pancreas_0_ENSG00000133466 | pancreas | gene | 22 | 37588013 | 0 | ENSG00000133466 | TRUE | 1.09947e-02 | FALSE | 182605 | FALSE |\n",
       "| 0.01000000 | 22_37588013_T_C | pancreas_1_ENSG00000133466 | pancreas | gene | 22 | 37588013 | 1 | ENSG00000133466 | TRUE | 1.09947e-02 | FALSE | 182605 | FALSE |\n",
       "| 0.39482843 | 22_37586792_C_T | islet_0_ENSG00000133466    | islet    | gene | 22 | 37586792 | 0 | ENSG00000133466 | TRUE | 1.22057e-02 | FALSE | 181384 | FALSE |\n",
       "| 0.01000000 | 22_37586792_C_T | islet_1_ENSG00000133466    | islet    | gene | 22 | 37586792 | 1 | ENSG00000133466 | TRUE | 1.22057e-02 | FALSE | 181384 | FALSE |\n",
       "| 0.01000000 | 22_37575695_A_G | pancreas_1_ENSG00000189060 | pancreas | gene | 22 | 37575695 | 1 | ENSG00000189060 | TRUE | 4.41465e-03 | FALSE | 170287 | FALSE |\n",
       "| 0.04858043 | 22_37271013_G_C | pancreas_0_ENSG00000100368 | pancreas | gene | 22 | 37271013 | 0 | ENSG00000100368 | TRUE | 3.18595e-03 | FALSE | 134395 | FALSE |\n",
       "| 0.01000000 | 22_37271013_G_C | pancreas_1_ENSG00000100368 | pancreas | gene | 22 | 37271013 | 1 | ENSG00000100368 | TRUE | 3.18595e-03 | FALSE | 134395 | FALSE |\n",
       "| 0.20768965 | 22_37276250_T_A | islet_0_ENSG00000100368    | islet    | gene | 22 | 37276250 | 0 | ENSG00000100368 | TRUE | 1.71799e-04 | FALSE | 129158 | FALSE |\n",
       "| 0.01000000 | 22_37276250_T_A | islet_1_ENSG00000100368    | islet    | gene | 22 | 37276250 | 1 | ENSG00000100368 | TRUE | 1.71799e-04 | FALSE | 129158 | FALSE |\n",
       "| 0.01000000 | 22_37421702_A_G | pancreas_2_ENSG00000100350 | pancreas | gene | 22 | 37421702 | 2 | ENSG00000100350 | TRUE | 2.49063e-03 | FALSE |  16294 | FALSE |\n",
       "| 0.05709081 | 22_37400957_G_A | pancreas_0_ENSG00000128311 | pancreas | gene | 22 | 37400957 | 0 | ENSG00000128311 | TRUE | 9.17943e-02 | FALSE |   4451 | FALSE |\n",
       "| 0.01000000 | 22_37400957_G_A | pancreas_1_ENSG00000128311 | pancreas | gene | 22 | 37400957 | 1 | ENSG00000128311 | TRUE | 9.17943e-02 | FALSE |   4451 | FALSE |\n",
       "\n"
      ],
      "text/plain": [
       "   SNP.PP     snp             qtl_id                     tissue   analysis\n",
       "1  0.07657956 22_36905776_G_A pancreas_0_ENSG00000100350 pancreas gene    \n",
       "2  0.05335938 22_36914347_C_G islet_0_ENSG00000100348    islet    gene    \n",
       "3  0.01000000 22_36914347_C_G islet_1_ENSG00000100348    islet    gene    \n",
       "4  0.01000000 22_37008928_A_G pancreas_2_ENSG00000100360 pancreas gene    \n",
       "5  0.29685296 22_37798644_G_T islet_0_ENSG00000166897    islet    gene    \n",
       "6  0.01000000 22_37798644_G_T islet_1_ENSG00000166897    islet    gene    \n",
       "7  0.09109036 22_37797138_A_G pancreas_0_ENSG00000166897 pancreas gene    \n",
       "8  0.01000000 22_37797138_A_G pancreas_1_ENSG00000166897 pancreas gene    \n",
       "9  0.04098229 22_37727452_C_G ipsc-ppc_0_ENSG00000189060 ipsc-ppc gene    \n",
       "10 0.47984539 22_37722890_A_G ipsc-ppc_0_ENSG00000166897 ipsc-ppc gene    \n",
       "11 0.04123813 22_37682496_G_C pancreas_0_ENSG00000100055 pancreas gene    \n",
       "12 0.01000000 22_37682496_G_C pancreas_1_ENSG00000100055 pancreas gene    \n",
       "13 0.18274071 22_37170977_C_T pancreas_0_ENSG00000100360 pancreas gene    \n",
       "14 0.01000000 22_37170977_C_T pancreas_1_ENSG00000100360 pancreas gene    \n",
       "15 0.16038611 22_37171611_A_G ipsc-ppc_0_ENSG00000100360 ipsc-ppc gene    \n",
       "16 0.01000000 22_37638046_T_C pancreas_1_ENSG00000128309 pancreas gene    \n",
       "17 0.01000000 22_37588820_A_G pancreas_2_ENSG00000133466 pancreas gene    \n",
       "18 0.12731895 22_37588013_T_C pancreas_0_ENSG00000133466 pancreas gene    \n",
       "19 0.01000000 22_37588013_T_C pancreas_1_ENSG00000133466 pancreas gene    \n",
       "20 0.39482843 22_37586792_C_T islet_0_ENSG00000133466    islet    gene    \n",
       "21 0.01000000 22_37586792_C_T islet_1_ENSG00000133466    islet    gene    \n",
       "22 0.01000000 22_37575695_A_G pancreas_1_ENSG00000189060 pancreas gene    \n",
       "23 0.04858043 22_37271013_G_C pancreas_0_ENSG00000100368 pancreas gene    \n",
       "24 0.01000000 22_37271013_G_C pancreas_1_ENSG00000100368 pancreas gene    \n",
       "25 0.20768965 22_37276250_T_A islet_0_ENSG00000100368    islet    gene    \n",
       "26 0.01000000 22_37276250_T_A islet_1_ENSG00000100368    islet    gene    \n",
       "27 0.01000000 22_37421702_A_G pancreas_2_ENSG00000100350 pancreas gene    \n",
       "28 0.05709081 22_37400957_G_A pancreas_0_ENSG00000128311 pancreas gene    \n",
       "29 0.01000000 22_37400957_G_A pancreas_1_ENSG00000128311 pancreas gene    \n",
       "   chrom pos      type transcript_id   in_1kg r2          in_r2 in_window in_ld\n",
       "1  22    36905776 0    ENSG00000100350 TRUE   5.23631e-02 FALSE 499632    FALSE\n",
       "2  22    36914347 0    ENSG00000100348 TRUE   3.93315e-06 FALSE 491061    FALSE\n",
       "3  22    36914347 1    ENSG00000100348 TRUE   3.93315e-06 FALSE 491061    FALSE\n",
       "4  22    37008928 2    ENSG00000100360 TRUE   7.11913e-06 FALSE 396480    FALSE\n",
       "5  22    37798644 0    ENSG00000166897 TRUE   1.07757e-03 FALSE 393236    FALSE\n",
       "6  22    37798644 1    ENSG00000166897 TRUE   1.07757e-03 FALSE 393236    FALSE\n",
       "7  22    37797138 0    ENSG00000166897 TRUE   1.45221e-04 FALSE 391730    FALSE\n",
       "8  22    37797138 1    ENSG00000166897 TRUE   1.45221e-04 FALSE 391730    FALSE\n",
       "9  22    37727452 0    ENSG00000189060 TRUE   3.64086e-03 FALSE 322044    FALSE\n",
       "10 22    37722890 0    ENSG00000166897 TRUE   2.09514e-03 FALSE 317482    FALSE\n",
       "11 22    37682496 0    ENSG00000100055 TRUE   9.20931e-04 FALSE 277088    FALSE\n",
       "12 22    37682496 1    ENSG00000100055 TRUE   9.20931e-04 FALSE 277088    FALSE\n",
       "13 22    37170977 0    ENSG00000100360 TRUE   1.72029e-02 FALSE 234431    FALSE\n",
       "14 22    37170977 1    ENSG00000100360 TRUE   1.72029e-02 FALSE 234431    FALSE\n",
       "15 22    37171611 0    ENSG00000100360 TRUE   3.71433e-03 FALSE 233797    FALSE\n",
       "16 22    37638046 1    ENSG00000128309 TRUE   4.71481e-04 FALSE 232638    FALSE\n",
       "17 22    37588820 2    ENSG00000133466 TRUE   4.36880e-04 FALSE 183412    FALSE\n",
       "18 22    37588013 0    ENSG00000133466 TRUE   1.09947e-02 FALSE 182605    FALSE\n",
       "19 22    37588013 1    ENSG00000133466 TRUE   1.09947e-02 FALSE 182605    FALSE\n",
       "20 22    37586792 0    ENSG00000133466 TRUE   1.22057e-02 FALSE 181384    FALSE\n",
       "21 22    37586792 1    ENSG00000133466 TRUE   1.22057e-02 FALSE 181384    FALSE\n",
       "22 22    37575695 1    ENSG00000189060 TRUE   4.41465e-03 FALSE 170287    FALSE\n",
       "23 22    37271013 0    ENSG00000100368 TRUE   3.18595e-03 FALSE 134395    FALSE\n",
       "24 22    37271013 1    ENSG00000100368 TRUE   3.18595e-03 FALSE 134395    FALSE\n",
       "25 22    37276250 0    ENSG00000100368 TRUE   1.71799e-04 FALSE 129158    FALSE\n",
       "26 22    37276250 1    ENSG00000100368 TRUE   1.71799e-04 FALSE 129158    FALSE\n",
       "27 22    37421702 2    ENSG00000100350 TRUE   2.49063e-03 FALSE  16294    FALSE\n",
       "28 22    37400957 0    ENSG00000128311 TRUE   9.17943e-02 FALSE   4451    FALSE\n",
       "29 22    37400957 1    ENSG00000128311 TRUE   9.17943e-02 FALSE   4451    FALSE"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 1 × 28</caption>\n",
       "<thead>\n",
       "\t<tr><th></th><th scope=col>eqtl_id</th><th scope=col>module_id</th><th scope=col>transcript_id</th><th scope=col>gene_id</th><th scope=col>gene_name</th><th scope=col>developmental_stage</th><th scope=col>tissue</th><th scope=col>eqtl_phenotype</th><th scope=col>eqtl_type</th><th scope=col>expressed_ipsc_ppc</th><th scope=col>⋯</th><th scope=col>pancreas_egene_overlap</th><th scope=col>module_pass</th><th scope=col>category_annotation</th><th scope=col>notes</th><th scope=col>chrom</th><th scope=col>analysis</th><th scope=col>taskid</th><th scope=col>ld_ipsc_ppc</th><th scope=col>ld_islet</th><th scope=col>ld_pancreas</th></tr>\n",
       "\t<tr><th></th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;lgl&gt;</th><th scope=col>⋯</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;lgl&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;int&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;int&gt;</th><th scope=col>&lt;lgl&gt;</th><th scope=col>&lt;lgl&gt;</th><th scope=col>&lt;lgl&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><th scope=row>1041</th><td>ipsc-ppc_0_ENSG00000128311</td><td>ipsc-ppc_0_ENSG00000128311</td><td>ENSG00000128311</td><td>ENSG00000128311</td><td>TST</td><td>fetal-like</td><td>ipsc-ppc</td><td>gene_expression</td><td>singleton</td><td>TRUE</td><td>⋯</td><td></td><td>FALSE</td><td>ambiguous</td><td>In LD with another eQTL</td><td>22</td><td>gene</td><td>1041</td><td>FALSE</td><td>FALSE</td><td>FALSE</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 1 × 28\n",
       "\\begin{tabular}{r|lllllllllllllllllllll}\n",
       "  & eqtl\\_id & module\\_id & transcript\\_id & gene\\_id & gene\\_name & developmental\\_stage & tissue & eqtl\\_phenotype & eqtl\\_type & expressed\\_ipsc\\_ppc & ⋯ & pancreas\\_egene\\_overlap & module\\_pass & category\\_annotation & notes & chrom & analysis & taskid & ld\\_ipsc\\_ppc & ld\\_islet & ld\\_pancreas\\\\\n",
       "  & <chr> & <chr> & <chr> & <chr> & <chr> & <chr> & <chr> & <chr> & <chr> & <lgl> & ⋯ & <chr> & <lgl> & <chr> & <chr> & <int> & <chr> & <int> & <lgl> & <lgl> & <lgl>\\\\\n",
       "\\hline\n",
       "\t1041 & ipsc-ppc\\_0\\_ENSG00000128311 & ipsc-ppc\\_0\\_ENSG00000128311 & ENSG00000128311 & ENSG00000128311 & TST & fetal-like & ipsc-ppc & gene\\_expression & singleton & TRUE & ⋯ &  & FALSE & ambiguous & In LD with another eQTL & 22 & gene & 1041 & FALSE & FALSE & FALSE\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 1 × 28\n",
       "\n",
       "| <!--/--> | eqtl_id &lt;chr&gt; | module_id &lt;chr&gt; | transcript_id &lt;chr&gt; | gene_id &lt;chr&gt; | gene_name &lt;chr&gt; | developmental_stage &lt;chr&gt; | tissue &lt;chr&gt; | eqtl_phenotype &lt;chr&gt; | eqtl_type &lt;chr&gt; | expressed_ipsc_ppc &lt;lgl&gt; | ⋯ ⋯ | pancreas_egene_overlap &lt;chr&gt; | module_pass &lt;lgl&gt; | category_annotation &lt;chr&gt; | notes &lt;chr&gt; | chrom &lt;int&gt; | analysis &lt;chr&gt; | taskid &lt;int&gt; | ld_ipsc_ppc &lt;lgl&gt; | ld_islet &lt;lgl&gt; | ld_pancreas &lt;lgl&gt; |\n",
       "|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n",
       "| 1041 | ipsc-ppc_0_ENSG00000128311 | ipsc-ppc_0_ENSG00000128311 | ENSG00000128311 | ENSG00000128311 | TST | fetal-like | ipsc-ppc | gene_expression | singleton | TRUE | ⋯ | <!----> | FALSE | ambiguous | In LD with another eQTL | 22 | gene | 1041 | FALSE | FALSE | FALSE |\n",
       "\n"
      ],
      "text/plain": [
       "     eqtl_id                    module_id                  transcript_id  \n",
       "1041 ipsc-ppc_0_ENSG00000128311 ipsc-ppc_0_ENSG00000128311 ENSG00000128311\n",
       "     gene_id         gene_name developmental_stage tissue   eqtl_phenotype \n",
       "1041 ENSG00000128311 TST       fetal-like          ipsc-ppc gene_expression\n",
       "     eqtl_type expressed_ipsc_ppc ⋯ pancreas_egene_overlap module_pass\n",
       "1041 singleton TRUE               ⋯                        FALSE      \n",
       "     category_annotation notes                   chrom analysis taskid\n",
       "1041 ambiguous           In LD with another eQTL 22    gene     1041  \n",
       "     ld_ipsc_ppc ld_islet ld_pancreas\n",
       "1041 FALSE       FALSE    FALSE      "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "out = qtl2class[row,]\n",
    "\n",
    "qtlid      = qtl2class[row,]$eqtl_id\n",
    "tiss       = qtl2class[row,]$tissue\n",
    "mod_id     = qtl2class[row,]$module_id\n",
    "phen       = qtl2class[row,]$analysis\n",
    "snp        = top[top$qtl_id == qtlid,]$snp\n",
    "position   = as.numeric(unlist(strsplit(snp, \"_\"))[2])\n",
    "eqtltype   = qtl2class[row,]$eqtl_type\n",
    "transcript = unlist(strsplit(qtlid, \"_\"))\n",
    "transcript = paste(transcript[3:length(transcript)], collapse = \"_\")\n",
    "\n",
    "message(paste(\"qtl_id:\"    , qtlid))\n",
    "message(paste(\"qtltype:\"   , eqtltype))\n",
    "message(paste(\"transcript:\", transcript))\n",
    "message(paste(\"phenotype:\" , phen))\n",
    "message(paste(\"snp:\"       , snp))\n",
    "\n",
    "chr = unlist(strsplit(snp, \"_\"))[1]\n",
    "message(paste(\"chromosome:\", chr))\n",
    "\n",
    "# Get eqtls that won't be tested, i.e. other conditionals of the same transcript or other eqtls in the same module\n",
    "qtls1 = qtl2class[qtl2class$analysis == phen & \n",
    "                  qtl2class$chrom == chr & \n",
    "                  qtl2class$tissue == tiss & \n",
    "                  (qtl2class$eqtl_id %like% transcript | qtl2class$module_id == mod_id),]$eqtl_id\n",
    "\n",
    "qtls = unique(c(qtls1, qtl2class[qtl2class$analysis == phen & qtl2class$module_id == mod_id,]$eqtl_id))\n",
    "message(paste(\"Do not test these qtls:\", paste(qtls, collapse = \"\\n\"), sep = \"\\n\"))\n",
    "\n",
    "message(\"Testing these QTLs for LD within 500 kb: ================\")\n",
    "to_test = top %>% filter(chrom == chr & analysis == phen & !qtl_id %in% qtls & pos >= (position - 500e3) & pos <= (position + 500e3))\n",
    "\n",
    "if (tiss %in% c(\"pancreas\", \"islet\"))\n",
    "{\n",
    "    remove = to_test %>% filter(qtl_id %like% transcript & tissue == tiss)\n",
    "    print(paste(\"Removed:\", paste(remove$qtl_id, collapse = \", \")))\n",
    "    to_test = to_test %>% filter(!qtl_id %in% remove$qtl_id)\n",
    "}\n",
    "\n",
    "message(\"Opening LD\")\n",
    "bim = fread(paste(\"reviews/LD/r2/out\", paste0(\"chr\", chr, \".bim\"), sep = \"/\"), data.table = F)\n",
    "ld = fread(paste(\"reviews/LD/r2/out\", paste0(\"chr\", chr, \".ld\"), sep = \"/\"), data.table = F)\n",
    "rownames(ld) = bim$V2\n",
    "colnames(ld) = bim$V2\n",
    "\n",
    "ld = suppressWarnings(suppressMessages(melt(as.matrix(ld))))\n",
    "\n",
    "ld2 = ld %>% filter(Var1 == snp & Var2 %in% to_test$snp)\n",
    "print(summary(ld2$value))\n",
    "to_test$r2 = as.numeric(suppressMessages(mapvalues(to_test$snp, from = ld2$Var2, to = ld2$value)))\n",
    "to_test$in_r2 = ifelse(to_test$snp %in% ld2$Var2, ifelse(to_test$snp %in% ld2[ld2$value >= 0.2,]$Var2, T, F), \"not_tested\")\n",
    "to_test$in_window = abs(to_test$pos - position)\n",
    "to_test$in_ld = ifelse(to_test$in_r2 == T, T, ifelse(to_test$in_r2 == \"not_tested\" & to_test$in_window <= 500e3, T, F))\n",
    "table(to_test$tissue, to_test$in_ld)\n",
    "\n",
    "message(\"Annotated for LD\")\n",
    "# to_test %>% arrange(in_r2)\n",
    "# to_test %>% arrange(desc(SNP.PP))\n",
    "to_test %>% arrange(desc(in_window))\n",
    "\n",
    "out$ld_ipsc_ppc = ifelse(nrow(to_test[to_test$tissue == \"ipsc-ppc\" & to_test$in_ld == T,]) > 0, T, F)\n",
    "out$ld_islet = ifelse(nrow(to_test[to_test$tissue == \"islet\" & to_test$in_ld == T,]) > 0, T, F)\n",
    "out$ld_pancreas = ifelse(nrow(to_test[to_test$tissue == \"pancreas\" & to_test$in_ld == T,]) > 0, T, F)\n",
    "\n",
    "return(out)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Get eQTL annotations before LD analysis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 804,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading objects:\n",
      "  graphlist\n"
     ]
    }
   ],
   "source": [
    "load(\"pipeline/3.4.coloc_adult/summary/panc_islets.coloc_list.pp0.8.all_panc.gt1.robj\", verbose = T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 805,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "      adult-shared        fetal-adult        fetal-islet     fetal-pancreas \n",
       "              3413               4072               1015               1657 \n",
       "ipsc_ppc singleton    ipsc_ppc-unique    islet singleton       islet-unique \n",
       "              4528                815               4453                961 \n",
       "pancreas singleton    pancreas-unique \n",
       "              5648                822 "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 6 × 3</caption>\n",
       "<thead>\n",
       "\t<tr><th></th><th scope=col>eqtl_id</th><th scope=col>tissue</th><th scope=col>category_before_ld</th></tr>\n",
       "\t<tr><th></th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><th scope=row>1</th><td>ipsc-ppc_0_ENSG00000000419</td><td>ipsc_ppc</td><td>ipsc_ppc singleton</td></tr>\n",
       "\t<tr><th scope=row>2</th><td>ipsc-ppc_0_ENSG00000002726</td><td>ipsc_ppc</td><td>ipsc_ppc singleton</td></tr>\n",
       "\t<tr><th scope=row>3</th><td>ipsc-ppc_0_ENSG00000003436</td><td>ipsc_ppc</td><td>ipsc_ppc singleton</td></tr>\n",
       "\t<tr><th scope=row>4</th><td>ipsc-ppc_0_ENSG00000003987</td><td>ipsc_ppc</td><td>ipsc_ppc singleton</td></tr>\n",
       "\t<tr><th scope=row>5</th><td>ipsc-ppc_0_ENSG00000005102</td><td>ipsc_ppc</td><td>ipsc_ppc singleton</td></tr>\n",
       "\t<tr><th scope=row>6</th><td>ipsc-ppc_0_ENSG00000005194</td><td>ipsc_ppc</td><td>ipsc_ppc singleton</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 6 × 3\n",
       "\\begin{tabular}{r|lll}\n",
       "  & eqtl\\_id & tissue & category\\_before\\_ld\\\\\n",
       "  & <chr> & <chr> & <chr>\\\\\n",
       "\\hline\n",
       "\t1 & ipsc-ppc\\_0\\_ENSG00000000419 & ipsc\\_ppc & ipsc\\_ppc singleton\\\\\n",
       "\t2 & ipsc-ppc\\_0\\_ENSG00000002726 & ipsc\\_ppc & ipsc\\_ppc singleton\\\\\n",
       "\t3 & ipsc-ppc\\_0\\_ENSG00000003436 & ipsc\\_ppc & ipsc\\_ppc singleton\\\\\n",
       "\t4 & ipsc-ppc\\_0\\_ENSG00000003987 & ipsc\\_ppc & ipsc\\_ppc singleton\\\\\n",
       "\t5 & ipsc-ppc\\_0\\_ENSG00000005102 & ipsc\\_ppc & ipsc\\_ppc singleton\\\\\n",
       "\t6 & ipsc-ppc\\_0\\_ENSG00000005194 & ipsc\\_ppc & ipsc\\_ppc singleton\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 6 × 3\n",
       "\n",
       "| <!--/--> | eqtl_id &lt;chr&gt; | tissue &lt;chr&gt; | category_before_ld &lt;chr&gt; |\n",
       "|---|---|---|---|\n",
       "| 1 | ipsc-ppc_0_ENSG00000000419 | ipsc_ppc | ipsc_ppc singleton |\n",
       "| 2 | ipsc-ppc_0_ENSG00000002726 | ipsc_ppc | ipsc_ppc singleton |\n",
       "| 3 | ipsc-ppc_0_ENSG00000003436 | ipsc_ppc | ipsc_ppc singleton |\n",
       "| 4 | ipsc-ppc_0_ENSG00000003987 | ipsc_ppc | ipsc_ppc singleton |\n",
       "| 5 | ipsc-ppc_0_ENSG00000005102 | ipsc_ppc | ipsc_ppc singleton |\n",
       "| 6 | ipsc-ppc_0_ENSG00000005194 | ipsc_ppc | ipsc_ppc singleton |\n",
       "\n"
      ],
      "text/plain": [
       "  eqtl_id                    tissue   category_before_ld\n",
       "1 ipsc-ppc_0_ENSG00000000419 ipsc_ppc ipsc_ppc singleton\n",
       "2 ipsc-ppc_0_ENSG00000002726 ipsc_ppc ipsc_ppc singleton\n",
       "3 ipsc-ppc_0_ENSG00000003436 ipsc_ppc ipsc_ppc singleton\n",
       "4 ipsc-ppc_0_ENSG00000003987 ipsc_ppc ipsc_ppc singleton\n",
       "5 ipsc-ppc_0_ENSG00000005102 ipsc_ppc ipsc_ppc singleton\n",
       "6 ipsc-ppc_0_ENSG00000005194 ipsc_ppc ipsc_ppc singleton"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "before_ld = graphlist$qtl2class %>% select(eqtl_id, tissue, category_before_ld)\n",
    "\n",
    "# rename eqtl\n",
    "before_ld$eqtl_id = gsub(\" \", \"_\", before_ld$eqtl_id)\n",
    "before_ld$eqtl_id = ifelse(before_ld$tissue == \"ipsc_ppc\", paste0(\"ipsc-ppc_\", before_ld$eqtl_id), before_ld$eqtl_id)\n",
    "before_ld$eqtl_id = gsub(\"Pancreas\", \"pancreas_0\", before_ld$eqtl_id)\n",
    "before_ld$eqtl_id = gsub(\"Islets\", \"islet_0\", before_ld$eqtl_id)\n",
    "\n",
    "# rename tissue\n",
    "before_ld$category_before_ld = gsub(\"endocrine\", \"islet\", before_ld$category_before_ld)\n",
    "before_ld$category_before_ld = gsub(\"exocrine\", \"pancreas\", before_ld$category_before_ld)\n",
    "\n",
    "# rename some annotations\n",
    "before_ld$category_before_ld = ifelse(before_ld$category_before_ld == \"ppc-unique\", \"ipsc_ppc-unique\", before_ld$category_before_ld)\n",
    "before_ld$category_before_ld = ifelse(before_ld$category_before_ld == \"panc-unique\", \"pancreas-unique\", before_ld$category_before_ld)\n",
    "before_ld$category_before_ld = ifelse(before_ld$category_before_ld == \"adult-unique\", \"adult-shared\", before_ld$category_before_ld)\n",
    "\n",
    "# eqtl annotations before applying LD filter\n",
    "table(before_ld$category_before_ld)\n",
    "\n",
    "\n",
    "head(before_ld)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Get processed LD results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    - This table tells for each eQTL, whether it's in LD with an iPSC-PPC eQTL, LD with an islet eQTL, and LD with a pancreas eQTL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1002,
   "metadata": {},
   "outputs": [],
   "source": [
    "out = as.data.frame(rbindlist(lapply(c(1:22), function(taskid)\n",
    "{\n",
    "    file = paste(\"reviews/LD/r2/processed\", paste0(\"chr\", taskid, \".txt\"), sep = \"/\")\n",
    "    fread(file, data.table = F)\n",
    "})))\n",
    "\n",
    "# merge annotations before LD\n",
    "out = merge(out, before_ld %>% select(-tissue), by = \"eqtl_id\", all.x = T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1003,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>\n",
       ".list-inline {list-style: none; margin:0; padding: 0}\n",
       ".list-inline>li {display: inline-block}\n",
       ".list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n",
       "</style>\n",
       "<ol class=list-inline><li>'eqtl_id'</li><li>'module_id'</li><li>'transcript_id'</li><li>'gene_id'</li><li>'gene_name'</li><li>'developmental_stage'</li><li>'tissue'</li><li>'eqtl_phenotype'</li><li>'eqtl_type'</li><li>'expressed_ipsc_ppc'</li><li>'expressed_endocrine'</li><li>'expressed_exocrine'</li><li>'LD_ipsc_ppc'</li><li>'LD_islets'</li><li>'LD_pancreas'</li><li>'LD_islets_conditional'</li><li>'LD_pancreas_conditional'</li><li>'islet_egene_overlap'</li><li>'pancreas_egene_overlap'</li><li>'module_pass'</li><li>'category_annotation'</li><li>'notes'</li><li>'chrom'</li><li>'analysis'</li><li>'taskid'</li><li>'ld_ipsc_ppc'</li><li>'ld_islet'</li><li>'ld_pancreas'</li><li>'category_before_ld'</li></ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 'eqtl\\_id'\n",
       "\\item 'module\\_id'\n",
       "\\item 'transcript\\_id'\n",
       "\\item 'gene\\_id'\n",
       "\\item 'gene\\_name'\n",
       "\\item 'developmental\\_stage'\n",
       "\\item 'tissue'\n",
       "\\item 'eqtl\\_phenotype'\n",
       "\\item 'eqtl\\_type'\n",
       "\\item 'expressed\\_ipsc\\_ppc'\n",
       "\\item 'expressed\\_endocrine'\n",
       "\\item 'expressed\\_exocrine'\n",
       "\\item 'LD\\_ipsc\\_ppc'\n",
       "\\item 'LD\\_islets'\n",
       "\\item 'LD\\_pancreas'\n",
       "\\item 'LD\\_islets\\_conditional'\n",
       "\\item 'LD\\_pancreas\\_conditional'\n",
       "\\item 'islet\\_egene\\_overlap'\n",
       "\\item 'pancreas\\_egene\\_overlap'\n",
       "\\item 'module\\_pass'\n",
       "\\item 'category\\_annotation'\n",
       "\\item 'notes'\n",
       "\\item 'chrom'\n",
       "\\item 'analysis'\n",
       "\\item 'taskid'\n",
       "\\item 'ld\\_ipsc\\_ppc'\n",
       "\\item 'ld\\_islet'\n",
       "\\item 'ld\\_pancreas'\n",
       "\\item 'category\\_before\\_ld'\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 'eqtl_id'\n",
       "2. 'module_id'\n",
       "3. 'transcript_id'\n",
       "4. 'gene_id'\n",
       "5. 'gene_name'\n",
       "6. 'developmental_stage'\n",
       "7. 'tissue'\n",
       "8. 'eqtl_phenotype'\n",
       "9. 'eqtl_type'\n",
       "10. 'expressed_ipsc_ppc'\n",
       "11. 'expressed_endocrine'\n",
       "12. 'expressed_exocrine'\n",
       "13. 'LD_ipsc_ppc'\n",
       "14. 'LD_islets'\n",
       "15. 'LD_pancreas'\n",
       "16. 'LD_islets_conditional'\n",
       "17. 'LD_pancreas_conditional'\n",
       "18. 'islet_egene_overlap'\n",
       "19. 'pancreas_egene_overlap'\n",
       "20. 'module_pass'\n",
       "21. 'category_annotation'\n",
       "22. 'notes'\n",
       "23. 'chrom'\n",
       "24. 'analysis'\n",
       "25. 'taskid'\n",
       "26. 'ld_ipsc_ppc'\n",
       "27. 'ld_islet'\n",
       "28. 'ld_pancreas'\n",
       "29. 'category_before_ld'\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       " [1] \"eqtl_id\"                 \"module_id\"              \n",
       " [3] \"transcript_id\"           \"gene_id\"                \n",
       " [5] \"gene_name\"               \"developmental_stage\"    \n",
       " [7] \"tissue\"                  \"eqtl_phenotype\"         \n",
       " [9] \"eqtl_type\"               \"expressed_ipsc_ppc\"     \n",
       "[11] \"expressed_endocrine\"     \"expressed_exocrine\"     \n",
       "[13] \"LD_ipsc_ppc\"             \"LD_islets\"              \n",
       "[15] \"LD_pancreas\"             \"LD_islets_conditional\"  \n",
       "[17] \"LD_pancreas_conditional\" \"islet_egene_overlap\"    \n",
       "[19] \"pancreas_egene_overlap\"  \"module_pass\"            \n",
       "[21] \"category_annotation\"     \"notes\"                  \n",
       "[23] \"chrom\"                   \"analysis\"               \n",
       "[25] \"taskid\"                  \"ld_ipsc_ppc\"            \n",
       "[27] \"ld_islet\"                \"ld_pancreas\"            \n",
       "[29] \"category_before_ld\"     "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "colnames(out)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1004,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "      adult-shared        fetal-adult        fetal-islet     fetal-pancreas \n",
       "              3413               4072               1015               1657 \n",
       "ipsc_ppc singleton    ipsc_ppc-unique    islet singleton       islet-unique \n",
       "              4528                815               4453                961 \n",
       "pancreas singleton    pancreas-unique \n",
       "              5648                822 "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "table(out$category_before_ld)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Update annotations based on LD"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1005,
   "metadata": {},
   "outputs": [],
   "source": [
    "out$annotation = \"ambiguous\"\n",
    "out$annotation = ifelse(out$category_before_ld == \"adult-shared\" &\n",
    "                        out$ld_ipsc_ppc == F,\n",
    "                        \"adult-shared\",\n",
    "                        out$annotation)\n",
    "\n",
    "out$annotation = ifelse(out$category_before_ld == \"fetal-islet\" &\n",
    "                        out$ld_pancreas == F,\n",
    "                        \"fetal-islet\",\n",
    "                        out$annotation)\n",
    "\n",
    "out$annotation = ifelse(out$category_before_ld == \"fetal-pancreas\" &\n",
    "                        out$ld_islet == F,\n",
    "                        \"fetal-pancreas\",\n",
    "                        out$annotation)\n",
    "\n",
    "out$annotation = ifelse(out$category_before_ld == \"ipsc_ppc singleton\" &\n",
    "                        out$ld_islet == F &\n",
    "                        out$ld_ipsc_ppc == F &\n",
    "                        out$ld_pancreas == F,\n",
    "                        \"ipsc_ppc singleton\",\n",
    "                        out$annotation)\n",
    "\n",
    "out$annotation = ifelse(out$category_before_ld == \"ipsc_ppc-unique\" &\n",
    "                        out$ld_islet == F &\n",
    "                        out$ld_pancreas == F,\n",
    "                        \"ipsc_ppc-unique\",\n",
    "                        out$annotation)\n",
    "\n",
    "out$annotation = ifelse(out$category_before_ld == \"islet singleton\" &\n",
    "                        out$ld_islet == F &\n",
    "                        out$ld_ipsc_ppc == F &\n",
    "                        out$ld_pancreas == F,\n",
    "                        \"islet singleton\",\n",
    "                        out$annotation)\n",
    "\n",
    "out$annotation = ifelse(out$category_before_ld == \"islet-unique\" &\n",
    "                        out$ld_ipsc_ppc == F &\n",
    "                        out$ld_pancreas == F,\n",
    "                        \"islet-unique\",\n",
    "                        out$annotation)\n",
    "\n",
    "out$annotation = ifelse(out$category_before_ld == \"pancreas singleton\" &\n",
    "                        out$ld_ipsc_ppc == F &\n",
    "                        out$ld_islet == F &\n",
    "                        out$ld_pancreas == F,\n",
    "                        \"pancreas singleton\",\n",
    "                        out$annotation)\n",
    "\n",
    "out$annotation = ifelse(out$category_before_ld == \"pancreas-unique\" &\n",
    "                        out$ld_ipsc_ppc == F &\n",
    "                        out$ld_islet == F,\n",
    "                        \"pancreas-unique\",\n",
    "                        out$annotation)\n",
    "\n",
    "out$annotation = ifelse(out$category_before_ld == \"fetal-adult\",\n",
    "                        \"fetal-adult\",\n",
    "                        out$annotation)\n",
    "\n",
    "# if the eqtls are in a failed module, annotate as \"module_failed\"\n",
    "out$annotation = ifelse(out$module_pass == F & out$eqtl_type == \"combinatorial\", \"module_failed\", out$annotation)\n",
    "\n",
    "# update module_pass column to be \"NA\" for singletons\n",
    "out$module_pass = ifelse(out$eqtl_type == \"combinatorial\", out$module_pass, NA)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1006,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "      adult-shared          ambiguous        fetal-adult        fetal-islet \n",
       "              2221              12098               3841                555 \n",
       "    fetal-pancreas ipsc_ppc singleton    ipsc_ppc-unique    islet singleton \n",
       "               961               1518                302               2225 \n",
       "      islet-unique      module_failed pancreas singleton    pancreas-unique \n",
       "               548                330               2358                427 "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "table(out$annotation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1012,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "combinatorial     singleton \n",
       "        12755         14629 "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "table(out$eqtl_type)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1015,
   "metadata": {},
   "outputs": [],
   "source": [
    "out = out %>% dplyr::rename(category_after_ld = annotation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1027,
   "metadata": {},
   "outputs": [],
   "source": [
    "# annotate modules as ambiguous if at least one is in LD\n",
    "mod_amb = out %>% filter(category_after_ld == \"ambiguous\" & eqtl_type == \"combinatorial\") %>% select(module_id, eqtl_type) %>% distinct()\n",
    "out$category_after_ld = ifelse(out$module_id %in% mod_amb$module_id, \"ambiguous\", out$category_after_ld)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1028,
   "metadata": {},
   "outputs": [],
   "source": [
    "fwrite(out, \"reviews/tables/Table_S11_eQTL_Annotations_Final.txt\", row.names = F, sep = \"\\t\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1030,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "                    \n",
       "                     combinatorial singleton\n",
       "  adult-shared                2078         0\n",
       "  ambiguous                   3981      8528\n",
       "  fetal-adult                 3841         0\n",
       "  fetal-islet                  494         0\n",
       "  fetal-pancreas               839         0\n",
       "  ipsc_ppc singleton             0      1518\n",
       "  ipsc_ppc-unique              287         0\n",
       "  islet singleton                0      2225\n",
       "  islet-unique                 514         0\n",
       "  module_failed                330         0\n",
       "  pancreas singleton             0      2358\n",
       "  pancreas-unique              391         0"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "table(out$category_after_ld, out$eqtl_type)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1029,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "                      \n",
       "                       ipsc-ppc islet pancreas\n",
       "  alternative_splicing     3959  4939     2077\n",
       "  gene_expression          4149  3948     8312"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "table(out$eqtl_phenotype, out$tissue)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "R 4.2.1",
   "language": "R",
   "name": "ir421"
  },
  "language_info": {
   "codemirror_mode": "r",
   "file_extension": ".r",
   "mimetype": "text/x-r-source",
   "name": "R",
   "pygments_lexer": "r",
   "version": "4.2.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
